2010-10-01 136 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