1

I am trying to get a xml document back from the webserver that also supports php. It's something similar to what the traditional web services do but i want to achieve it in php. Is this even possible?

To be more specific about my needs - I want to send a xml document as a request to the server, have PHP do some processing on it and send me back an xml document as a response.

Thanks in advance.

VJ Vélan Solutions
  • 6,434
  • 5
  • 49
  • 63

3 Answers3

4

Maybe you simply want http://php.net/SOAP ?

If not SOAP, then you can send your XML POST request and use $xml = file_get_contents('php://input'); to dump it to a variable that you can feed to http://php.net/DOM or other XML processors.

After processing, you header('Content-Type: text/xml'); (or application/xml) and output the modified XML document.

Tino Didriksen
  • 2,215
  • 18
  • 21
  • 1
    why not use the [`http_get_request_body`](http://www.php.net/manual/en/function.http-get-request-body.php) and the other [`http_get_request_*` family of functions](http://www.php.net/manual/en/ref.http.php) instead of `php://input`? – prodigitalson Jul 21 '11 at 16:32
  • 1
    Because those are not standard functions; they require a PECL module that may not be available. php://input works everywhere. – Tino Didriksen Jul 21 '11 at 16:50
  • +1... True enough, but unless its an intranet it should be a non-issue. Ive havent deployed to a host that doesnt let one install PECL extensions or PEAR packages since ~2000. – prodigitalson Jul 21 '11 at 17:06
  • Thanks Tino, `file_get_contents('php://input');` worked a treat, the `http_get_request_body` function wasnt available in the server I was using. – Steven de Salas Mar 15 '12 at 18:18
0

Super simple example of reading an XML request body:

$request = http_get_request_body();
if($request && strpos($request, '<?xml') !== 0){
   // not XML do somehting appropriate
} else {
  $response = new DomDocment(); // easier to manipulate when *building* xml
  $requestData = DomDocument::load($request);
  // process $requestData however and build the $response XML

  $responseString = $response->saveXML();
  header('HTTP/1.1 200 OK');
  header('Content-type: application/xml');
  header('Content-length: ', strlen($responseString));
  print $responseString;
  exit(0);

}
prodigitalson
  • 60,050
  • 10
  • 100
  • 114
  • I am seeing "Call to undefined function http_get_request_body()" error reported by the server. I am running PHP Version 5.2.17 and i can access the phpinfo() but i am not sure what module is required to make the server stop complaining about http_get_request_body() not being found. Thanks – VJ Vélan Solutions Jul 21 '11 at 17:47
  • You need [`pecl_http`](http://www.php.net/manual/en/book.http.php). Alternatively you could replace that call with the `file_get_contents('php://input')` as was suggested by [Tino](http://stackoverflow.com/questions/6779320/php-send-and-receive-xml-documents/6779443#6779443) – prodigitalson Jul 21 '11 at 17:58
  • Thanks. But i don't think my hosting services have installed the pecl_http module. The only reference i find for pecl is the sqlite. But file_get_contents API worked. – VJ Vélan Solutions Jul 21 '11 at 20:35
-1

use curl.

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$xmlString);

//execute post
$result = curl_exec($ch);
marcelog
  • 7,062
  • 1
  • 33
  • 46