Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse SOAP XML?

Tags:

php

soap

xml

SOAP XML:

<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">   <soap:Body>     <PaymentNotification xmlns="http://apilistener.envoyservices.com">       <payment>         <uniqueReference>ESDEUR11039872</uniqueReference>               <epacsReference>74348dc0-cbf0-df11-b725-001ec9e61285</epacsReference>         <postingDate>2010-11-15T15:19:45</postingDate>         <bankCurrency>EUR</bankCurrency>         <bankAmount>1.00</bankAmount>         <appliedCurrency>EUR</appliedCurrency>         <appliedAmount>1.00</appliedAmount>         <countryCode>ES</countryCode>         <bankInformation>Sean Wood</bankInformation>   <merchantReference>ESDEUR11039872</merchantReference>    </payment>     </PaymentNotification>   </soap:Body> </soap:Envelope> 

How to get 'payment' element?

I try to parse (PHP)

$xml = simplexml_load_string($soap_response); $xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/'); foreach ($xml->xpath('//payment') as $item) {     print_r($item); } 

Result is empty :( Any ideas how to parse it correct?

like image 279
Anton Avatar asked Nov 16 '10 13:11

Anton


People also ask

What is parsing in SOAP?

The SOAP Parser reads and writes SOAP XML documents. The Parser converts SOAP XML documents to or from entry objects in a simple, straightforward fashion. When writing the XML document, the Parser uses attributes from the entry to build the document.

What is SOAP XML format?

A SOAP message is an ordinary XML document containing the following elements: An Envelope element that identifies the XML document as a SOAP message. A Header element that contains header information. A Body element that contains call and response information.


2 Answers

One of the simplest ways to handle namespace prefixes is simply to strip them from the XML response before passing it through to simplexml such as below:

$your_xml_response = '<Your XML here>'; $clean_xml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $your_xml_response); $xml = simplexml_load_string($clean_xml); 

This would return the following:

SimpleXMLElement Object (     [Body] => SimpleXMLElement Object         (             [PaymentNotification] => SimpleXMLElement Object                 (                     [payment] => SimpleXMLElement Object                         (                             [uniqueReference] => ESDEUR11039872                             [epacsReference] => 74348dc0-cbf0-df11-b725-001ec9e61285                             [postingDate] => 2010-11-15T15:19:45                             [bankCurrency] => EUR                             [bankAmount] => 1.00                             [appliedCurrency] => EUR                             [appliedAmount] => 1.00                             [countryCode] => ES                             [bankInformation] => Sean Wood                             [merchantReference] => ESDEUR11039872                         )                  )          )  ) 
like image 168
Aran Avatar answered Sep 21 '22 15:09

Aran


PHP version > 5.0 has a nice SoapClient integrated. Which doesn't require to parse response xml. Here's a quick example

$client = new SoapClient("http://path.to/wsdl?WSDL"); $res = $client->SoapFunction(array('param1'=>'value','param2'=>'value')); echo $res->PaymentNotification->payment; 
like image 36
Ivan Avatar answered Sep 20 '22 15:09

Ivan