2011-02-02 72 views
0

我想在PHP5中使用simeplexml類來處理一個小的XML文件。但是爲了獲得該文件,腳本必須向遠程服務器發送特定的POST請求,以便「給我」一個XML文件作爲回報。所以我相信我不能使用「simplexml_load_file」方法。該文件僅用於處理,然後可以,甚至應該刪除/刪除。這種類型的下一個與該怎麼做如何通過PHP5接收並處理通過POST請求的XML文件?

$header = 'POST '.$gateway.' HTTP/1.0'."\r\n" . 
      'Host: '.$server."\r\n". 
      'Content-Type: application/x-www-form-urlencoded'."\r\n". 
      'Content-Length: '.strlen($param)."\r\n". 
      'Connection: close'."\r\n\r\n"; 

並沒有太多的想法 我有HTTP標頭。有fsockopen,但我不確定這是否合適或如何去與它。

回答

1

我的建議是使用像Zend_Http_Client庫或cURL。通過fsockopen正確使用將會是一個很難調試的問題。

Zend_Http_Client有一個很好的界面,並會非常出色。

CURL並沒有太多的痛苦,它已經是大多數PHP構建的一部分。 以下示例:

$ch = curl_init(); 
// set URL and other appropriate options 
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/"); // Replace with your URL 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$output = curl_exec($ch) // Return the XML string of data 

// Parse output to Simple XML 
// You'll probably want to do some validation here to validate that the returned output is XML 
$xml = simplexml_load_string($output);