2010-10-01 121 views
0

我有一个PHP页面说test.php我怎么能发送XML通过URL

我在这里创建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?

+1

这取决于目标预计它的格式。HTTP PUT请求?使用XML作为给定字段名称的值来编码编码数据?还有别的吗? – Quentin 2010-10-01 23:06:48

回答

3

,卷曲

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

感谢您的详细解答。让我检查一下 – 2010-10-01 23:34:13

-1

也许你的XML数据不是很长,但它不是建议您发送使用此way.It数据指的是安全problem.So使用POST获取

忘了这个职位,我得到的东西错了...对不起:(

+0

如何发布比获取更安全? – 2010-10-02 01:12:34

+0

谁说了关于GET的一切? – Quentin 2010-10-02 08:05:53

+0

我如何在这里使用POST,你能解释更多吗? – 2010-10-04 15:51:12