如何在 PHP 中解析 SOAP 响应
过去几天我一直在试图解析肥皂响应(通过curl命令行),但我无法让它工作。我只想获取 ResourceIdentifier
的对象值,即 rs-1304500829200-200。我正在使用 PHP 5.3.x
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:deliverMDRecordsResponse xmlns:ns2="http://mdstore.data.dnetlib.eu/" xmlns:ns3="http://www.w3.org/2005/08/addressing">
<return>
<ns3:Address>http://129.70.12.20:8282/dnet-mdstore/services/MDStoreResultSet</ns3:Address>
<ns3:ReferenceParameters>
<ResourceIdentifier:ResourceIdentifier xmlns:ResourceIdentifier="http://www.driver.org" xmlns:wsa="http://www.w3.org/2005/08/addressing">rs-1304500829200-200</ResourceIdentifier:ResourceIdentifier>
</ns3:ReferenceParameters>
</return>
</ns2:deliverMDRecordsResponse>
</soap:Body>
</soap:Envelope>
<?
$response=<<<END ....soap response here... END;
$xml = simplexml_load_string($source);
$xml->registerXPathNamespace('t', 'http://www.driver.org');
foreach ($xml->xpath('//t:ResourceIdentifier') as $item)
{ // print_r($item);
echo $item->asXML();
}
?>
I have cracked my head the last few days trying to parse a soap response (via curl command-line), but I can not get it to work. I just want to get the object value of ResourceIdentifier
which is rs-1304500829200-200. I am using PHP 5.3.x
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:deliverMDRecordsResponse xmlns:ns2="http://mdstore.data.dnetlib.eu/" xmlns:ns3="http://www.w3.org/2005/08/addressing">
<return>
<ns3:Address>http://129.70.12.20:8282/dnet-mdstore/services/MDStoreResultSet</ns3:Address>
<ns3:ReferenceParameters>
<ResourceIdentifier:ResourceIdentifier xmlns:ResourceIdentifier="http://www.driver.org" xmlns:wsa="http://www.w3.org/2005/08/addressing">rs-1304500829200-200</ResourceIdentifier:ResourceIdentifier>
</ns3:ReferenceParameters>
</return>
</ns2:deliverMDRecordsResponse>
</soap:Body>
</soap:Envelope>
<?
$response=<<<END ....soap response here... END;
$xml = simplexml_load_string($source);
$xml->registerXPathNamespace('t', 'http://www.driver.org');
foreach ($xml->xpath('//t:ResourceIdentifier') as $item)
{ // print_r($item);
echo $item->asXML();
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果可以的话,您确实应该使用内置的 SoapClient 类,或者如果不能,请使用 PEAR SOAP 库。当您运行 PHP 5.3 时,SoapClient 应该可用。
关于使用 Xpath 的问题,您正在查询错误的名称空间和元素。您正在查询的元素的命名空间是“http://www.driver.org”,因此这应该可以工作,但请记住我实际上还没有运行它,尽管它应该是正确的:
但是请不要'如果不这样做,请使用我提到的两个 SOAP 客户端之一。我不知道您从哪里获得
{http://apilistener.envoyservices.com} payment
,因为响应中没有提及。You should really be using the built in SoapClient class if you can, or if you can't, use the PEAR SOAP libraries. As you're running PHP 5.3, SoapClient ought to be available.
On the issue of your use of Xpath, you're querying against the wrong namespace and element. The namespace of the element you're querying is "http://www.driver.org", thus this should work, though keep in mind that I haven't actually ran it, though it should be correct:
But please don't do that, use one of the two SOAP clients I mentioned. I've no idea where you got
{http://apilistener.envoyservices.com}payment
from as it's not mentioned in the response.