php - 在 PHP 提交上从 URL 发送 XML

标签 php xml curl xml-parsing

让我们想象以下情况:

您有一个带有 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/

相关文章:

php类在函数内调用函数

php - 是否有可能从这样的日期 2011-02-27 02 :04:46? 获得 UNIX 时间

linux - 我可以使用 curl real time 减去 sys 和 user 来获得总响应时间吗

xml - 使用 XSLT 转换 Unicode 转义字符

使用 curl 为 PAM 模块编译目标文件

php - PayPal Express Checkout 始终显示用户 "This transaction has expired."页面,但没有 API 错误

javascript - 有没有办法将菜单链接放到该菜单链接的子菜单中,并在您按下另一个选项之前保持选定的颜色?

file-upload - PHP fatal error : Out of memory (allocated 80740352) (tried to allocate 12352 bytes) in

xml - XPath - (//first//*)[1] 与//first//*[1]

c# - 在 XmlDocument Load 之前是否仍然要删除 Ampersand?