如何在PHP中解析SOAP响应

时间:2011-05-04 11:53:08

标签: php soap soap-client

过去几天我试图解析一个肥皂响应(通过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();    
 }    
?>

1 个答案:

答案 0 :(得分:4)

如果可以,您应该使用内置SoapClient class,或者如果不能,则使用PEAR SOAP libraries。当你运行PHP 5.3时,SoapClient应该可用。

关于使用Xpath的问题,您要查询错误的命名空间和元素。你要查询的元素的名称空间是“http://www.driver.org”,因此这应该有用,但要记住我实际上没有运行它,尽管它应该是正确的:

<?php
$xml = simplexml_load_string($response); 
$xml->registerXPathNamespace('rid', 'http://www.driver.org');
foreach ($xml->xpath('//rid:ResourceIdentifier') as $item) {
    echo (string) $item; 
}
?>

但请不要这样做,使用我提到的两个SOAP客户端之一。我不知道你从哪里得到{http://apilistener.envoyservices.com}payment,因为答案中没有提及。