【问题标题】:how can i send a xml through a url我如何通过 url 发送 xml
【发布时间】:2010-10-01 23:04:01
【问题描述】:

我有一个 php 页面说 test.php

我在这里创建一个 xml

$xmlVariable = <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<signupInfo>
<address>
      <address>1 Infinite Loop</address>
      <city>Cupertino</city>
      <state>CA</state>
      <zip>99999</zip>
    </address>

</signupInfo>

现在我需要将它发送到目的地(eg:https://destination.cm/fg)

我如何发送这个 xml?

【问题讨论】:

  • 这取决于目的地期望它的格式。HTTP PUT 请求?使用 XML 作为给定字段名称的值的表单编码数据?还有什么?

标签: php xml


【解决方案1】:

使用卷曲

http://www.php.net/manual/en/ref.curl.php

$curl_handle = curl_init();
if (!$curl_handle) {
  die('fail to initialize');
}

curl_setopt($curl_handle, CURLOPT_TIMEOUT, 30);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 30);

//target URL setup
curl_setopt($curl_handle, CURLOPT_URL, 'https://destination.cm/fg');
//mime type setup, change if necessary
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array("Content-Type: application/xml"));

curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_FAILONERROR, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);

//here you assign the body of your request
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $xmlVariable);

$response = curl_exec($curl_handle);

if (curl_errno($curl_handle)) {
  die(curl_error($curl_handle));            
}

printf("Received :\n%s", $response);

【讨论】:

  • 感谢您的详细回答。让我检查一下
【解决方案2】:

可能你的xml数据不是很长,但是不建议用这种方式发送数据,属于安全问题,所以用POST代替get

忘记这篇文章,我搞错了......对不起:(

【讨论】:

  • post 怎么比 get 更安全?
  • 谁说过关于 GET 的事?
  • 我如何在这里使用 POST,你能解释一下吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-01
  • 1970-01-01
相关资源
最近更新 更多