0

我想在 PHP5 中使用 simeplexml 类来处理一个小的 XML 文件。但要获得该文件,脚本必须向远程服务器发送一个特定的 POST 请求,该服务器将“给”我一个 XML 文件作为回报。所以我相信我不能使用“simplexml_load_file”方法。该文件仅用于处理,然后它可以,甚至应该被删除/删除。我有这种类型的 HTTP HEADER

$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";

并且不知道下一步该怎么做。有 fsockopen 但我不确定这是否合适或如何使用它。

4

2 回答 2

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); 
于 2011-02-02T04:13:08.870 回答
0

我会使用诸如Zend_Http_Client之类的 HTTP 客户端库(如果您是受虐狂,则使用 cURL)来创建 POST 请求,然后将响应正文提供给simplexml_load_stringSimpleXMLElement::__construct()

于 2011-02-02T03:51:31.217 回答