让我们想象以下情况:
您有一个带有 html 表单的简单 php 页面,允许用户输入以下数据:
- 电话号码
- 短信
如:
<form action="xml-file.xml" method="get">
<input type="text" id="to" name="to" value="" />
<input type="text" id="text" name="text" value="" />
<input type="submit" />
</form>
现在,您使用 this 创建 XML我测试过的方法没有问题,之后你想使用 this other 通过 URL 发送它方法(CURL)。
不幸的是,我无法访问 CURL 方法,因为我无法将提交操作链接到既创建 XML 又将其发送到 URL 以便它转到本地服务器 (www.example.xxx:1234)然后处理该 XML 以发送 SMS。
如果 XML 格式正确,该服务器将发送响应。因此,我的问题是提交创建的 XML。
帮忙吗?
更新 1:添加更新的代码。这允许我(在 sms.xml 文件上使用 chmod 777)随意编辑 xml 文件。
index.php
<html>
...
<form action="send.php" method="get">
<input type="text" id="to" name="to" value="" />
<input type="text" id="text" name="text" value="" />
<input type="submit">
</form>
...
</html>
发送.php
<?php
$xml = simplexml_load_file("sms.xml"); // Load XML file
$xml->Title3 = $_GET['to']; // Updating <Title3> from GET method
$xml->Title5[0]->Title51Content = $_GET['text']; // Updating <Title51> from GET method
$xml->asXML('sms.xml'); // Saving the XML file
?>
sms.xml
<?xml version="1.0" encoding="UTF-8"?>
<Title1>
<Title2>Some Text</Title2>
<Title3>Variable 1</Title3>
<Title4>Some Text</Title4>
<Title5>
<Title51>Variable 2</Title51>
</Title5>
</Title1>
旁注:发送时,XML 应该有 "Content-Type","application/x-www-form-urlencoded"
header ,用于返回如下内容:
XML=%3C%3Fxml+version%3D%221.0%22+encoding%3D%22UTF-8%22%3F%3E%0A%3...
谢谢!
最佳答案
将 XML 作为字符串返回并发布?
...
$xml_post_string = 'XML='.urlencode($xml->asXML());
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://theurl.com');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
...
关于php - 在 PHP 提交上从 URL 发送 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12244989/