如何接收XML请求,并在PHP发送响应XML? [英] How to receive xml requests and send response xml in php?
问题描述
所以,我需要建立将接收XML请求,并根据我将不得不返回响应XML的应用程序。我知道如何发送请求和接收响应,但我从来没有做过的其他方式。我会发送请求,像这样:
So I need to build an application that will receive xml request and based on that I will have to return the response xml. I know how to send requests and receive the response, but I have never done it the other way. I would send the request like so:
private function sendRequest($requestXML)
{
$server = 'http://www.something.com/myapp';
$headers = array(
"Content-type: text/xml"
,"Content-length: ".strlen($requestXML)
,"Connection: close"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $server);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestXML);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);
if(curl_errno($ch)){
print curl_error($ch);
echo " something went wrong..... try later";
}else{
curl_close($ch);
}
return $data;
}
我的问题是 - 什么将是对接收方的code?我如何赶上传入的请求?
谢谢你。
My question is - what would be the code on the receiving side? How do I catch the incoming request? Thanks.
推荐答案
的总体思路是按照API您在POST值读取,解析为XML,就成为一个商业决策,打造出了一个XML响应已经决定上,并将其写入到响应。
The general idea is to read in the POST value, parse it as XML, make a business decision on it, build out an XML response according to the API you've decided on, and write it into the response.
阅读中的POST值:
$dataPOST = trim(file_get_contents('php://input'));
解析为XML:
$xmlData = simplexml_load_string($dataPOST);
然后,你就打造出了一个XML字符串(或文档树,如果你愿意的话),并打印出来到响应。打印()或回波()会做得很好。
Then, you would build out an XML string (or document tree, if you wish), and print it out to the response. print() or echo() will do fine.
这篇关于如何接收XML请求,并在PHP发送响应XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!