开发者

How to receive and handle an XML file requested through POST in PHP5?

开发者 https://www.devze.com 2023-02-08 14:00 出处:网络
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. S

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 wh开发者_C百科at to do next with that. There is fsockopen but I'm not sure if that would be appropriate or how to go with it.


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); 


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()

0

精彩评论

暂无评论...
验证码 换一张
取 消