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.

1

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

why not use the http_get_request_body and the other http_get_request_* family of functions instead of php://input?
Because those are not standard functions; they require a PECL module that may not be available. php://input works everywhere.
+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.
Thanks Tino, file_get_contents('php://input'); worked a treat, the http_get_request_body function wasnt available in the server I was using.
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);

}

3 Comments

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
You need pecl_http. Alternatively you could replace that call with the file_get_contents('php://input') as was suggested by Tino
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.
-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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.