我怎样才能通过网址发送xml

时间:2010-10-01 23:04:02

标签: php xml

我有一个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?

2 个答案:

答案 0 :(得分:3)

使用cURL

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);

答案 1 :(得分:-1)

也许您的xml数据不是很长,但并不建议您使用这种方式发送数据。它指的是安全问题。所以使用POST代替获取

忘了这篇文章,我弄错了...抱歉:(