2011-02-02 67 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);