[英]php - send and receive xml documents
我正在尝试从同样支持 php 的网络服务器获取 xml 文档。 这类似于传统的 web 服务所做的,但我想在 php 中实现它。 这甚至可能吗?
为了更具体地了解我的需求 - 我想将 xml 文档作为请求发送到服务器,让 PHP 对其进行一些处理,然后将 Z0F635D0E0F3874FFF8B581C132E6C7 文档作为响应发回给我。
提前致谢。
也许您只是想要http://php.net/SOAP ?
如果不是 SOAP,那么您可以发送您的 XML POST 请求并使用$xml = file_get_contents('php://input');
将其转储到一个变量,您可以将其提供给http://php.net/DOM或其他 XML 处理器。
处理后,你header('Content-Type: text/xml');
(或应用程序/xml)和 output 修改后的 XML 文档。
读取 XML 请求正文的超级简单示例:
$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);
}
使用 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.