How to parse SOAP response with PHP?

How to parse SOAP response in PHP

  • 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(); } ?>

  • Answer:

    You should really be using the built in http://www.php.net/manual/en/book.soap.php if you can, or if you can't, use the http://pear.php.net/package/SOAP. 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: <?php $xml = simplexml_load_string($response); $xml->registerXPathNamespace('rid', 'http://www.driver.org'); foreach ($xml->xpath('//rid:ResourceIdentifier') as $item) { echo (string) $item; } ?> 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.

ofuuzo at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.