PHP5中如何接收并处理通过POST请求的XML文件?

发布于 2024-10-15 13:46:05 字数 500 浏览 3 评论 0原文

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

$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 但我不确定这是否合适或如何使用它。

I want to use simeplexml class in PHP5 to handle a small XML file. But to obtain that file, script has to send a specific POST request to a remote server that will "give" me an XML file in return. So I believe I can't use the "simplexml_load_file" method. This file is needed just for processing, then it can, or even should, be gone/deleted.
I've got HTTP HEADER of this type

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

And not much idea of what to do next with that. There is fsockopen but I'm not sure if that would be appropriate or how to go with it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方。的韩爷 2024-10-22 13:46:05

我的建议是使用 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); 

My advice would be use something like Zend_Http_Client library or cURL. Getting everything right with fsockopen will be a pain to debug.

Zend_Http_Client has a nice interface and would work fabulously.

CURL isn't too much of a pain either and is already a part of most PHP builds.
Example below:

$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); 
十秒萌定你 2024-10-22 13:46:05

我会使用 HTTP 客户端库,例如 Zend_Http_Client (或 cURL如果您是受虐狂)创建 POST 请求,然后将响应正文输入 simplexml_load_stringSimpleXMLElement::__construct()

I'd use an HTTP client library like Zend_Http_Client (or cURL if you're a masochist) to create the POST request, then feed the response body into simplexml_load_string or SimpleXMLElement::__construct()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文