简体   繁体   中英

php - send and receive xml documents

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.

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.

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

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM