Link to home
Start Free TrialLog in
Avatar of mavmanau
mavmanauFlag for Australia

asked on

PHP XML SOAP - How to parse the Response?

Hi,

Firstly thank you for taking a look at my question your expert advice is very much appreciated.

I have the following response:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><GetPaymentsResponse xmlns="https://somedomain/"><GetPaymentsResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Data><Payment><BankFailedReason/><BankReceiptID>362021</BankReceiptID><BankReturnCode>O</BankReturnCode><CustomerName>JG testing</CustomerName><DebitDate>2012-08-24T00:00:00</DebitDate><EzidebitCustomerID/><InvoiceID>0</InvoiceID><PaymentAmount>100</PaymentAmount><PaymentID>WEB51409</PaymentID><PaymentMethod>CR</PaymentMethod><PaymentReference>JG123</PaymentReference><PaymentSource>WEB</PaymentSource><PaymentStatus>P</PaymentStatus><ScheduledAmount>97.42</ScheduledAmount><SettlementDate i:nil="true"/><TransactionFeeClient>0</TransactionFeeClient><TransactionFeeCustomer>2.58</TransactionFeeCustomer><TransactionTime>2012-08-25T10:38:00</TransactionTime><YourGeneralReference/><YourSystemReference/></Payment></Data><Error>0</Error><ErrorMessage i:nil="true"/></GetPaymentsResult></GetPaymentsResponse></s:Body></s:Envelope>

and the way i normally would do it in Simple_XML_response doesn't work as the soap is shortened to simply s:

do I still use simple_XML or is there another way?  your help/advice is much appreciated.

many thanks!
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Please use the code snippet feature to post the XML strings - it is much easier to read that way.
Avatar of mavmanau

ASKER

hi,

sorry, how is that?
<pre>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body><GetPaymentsResponse xmlns="https://px.ezidebit.com.au/">
<GetPaymentsResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Data><Payment>
<BankFailedReason/>
<BankReceiptID>362021</BankReceiptID>
<BankReturnCode>O</BankReturnCode>
<CustomerName>JG testing</CustomerName><DebitDate>2012-08-24T00:00:00</DebitDate>
<CustomerID/>
<InvoiceID>0</InvoiceID>
<PaymentAmount>100</PaymentAmount>
<PaymentID>WEB51409</PaymentID>
<PaymentMethod>CR</PaymentMethod>
<PaymentReference>JG123</PaymentReference>
<PaymentSource>WEB</PaymentSource>
<PaymentStatus>P</PaymentStatus>
<ScheduledAmount>97.42</ScheduledAmount>
<SettlementDate i:nil="true"/>
<TransactionFeeClient>0</TransactionFeeClient><TransactionFeeCustomer>2.58</TransactionFeeCustomer><TransactionTime>2012-08-25T10:38:00</TransactionTime>
<YourGeneralReference/><YourSystemReference/>
</Payment>
</Data>
<Error>0</Error>
<ErrorMessage i:nil="true"/>
</GetPaymentsResult>
</GetPaymentsResponse>
</s:Body>
</s:Envelope>

Open in new window

i can grab the individual parts once I know how to access them just having  a few issues getting the $obj->xxxxx as $data correct, unless I am better off going a different way to the simpleXML method?
This will throw some warnings because it appears that some of the information in the XML string has been blotted out, but it illustrates a way that you can deal with the namespace issue in a quick and easy way.  HTH, ~Ray

<?php // RAY_temp_mavmanau.php
error_reporting(E_ALL);
echo '<pre>';

// XML TEST DATA FROM THE POST AT EE
$xml = <<<XML
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><GetPaymentsResponse xmlns="https://somedomain/"><GetPaymentsResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Data><Payment><BankFailedReason/><BankReceiptID>362021</BankReceiptID><BankReturnCode>O</BankReturnCode><CustomerName>JG testing</CustomerName><DebitDate>2012-08-24T00:00:00</DebitDate><EzidebitCustomerID/><InvoiceID>0</InvoiceID><PaymentAmount>100</PaymentAmount><PaymentID>WEB51409</PaymentID><PaymentMethod>CR</PaymentMethod><PaymentReference>JG123</PaymentReference><PaymentSource>WEB</PaymentSource><PaymentStatus>P</PaymentStatus><ScheduledAmount>97.42</ScheduledAmount><SettlementDate i:nil="true"/><TransactionFeeClient>0</TransactionFeeClient><TransactionFeeCustomer>2.58</TransactionFeeCustomer><TransactionTime>2012-08-25T10:38:00</TransactionTime><YourGeneralReference/><YourSystemReference/></Payment></Data><Error>0</Error><ErrorMessage i:nil="true"/></GetPaymentsResult></GetPaymentsResponse></s:Body></s:Envelope>
XML;

// FUNCTION TO MUNG THE XML
function mungXML($xml)
{
    // A REGULAR EXPRESSION TO MUNG THE XML
	$rgx
	= '#'           // REGEX DELIMITER
	. '('           // GROUP PATTERN 1
	. '\<'          // LOCATE A LEFT WICKET
	. '/{0,1}'      // MAYBE FOLLOWED BY A SLASH
	. '.*?'         // ANYTHING OR NOTHING
	. ')'           // END GROUP PATTERN
	. '('           // GROUP PATTERN 2
	. ':{1}'        // A COLON (EXACTLY ONE)
	. ')'           // END GROUP PATTERN
	. '#'           // REGEX DELIMITER
	;
	// INSERT THE UNDERSCORE INTO THE TAG NAME
	$rep
	= '$1'          // BACKREFERENCE TO GROUP 1
	. '_'           // LITERAL UNDERSCORE IN PLACE OF GROUP 2
	;
	// PERFORM THE REPLACEMENT
	return preg_replace($rgx, $rep, $xml);
}

// MAKE AN OBJECT
$obj = SimpleXML_Load_String(mungXML($xml));
var_dump($obj);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
many thanks for your assistance!! So quick as well!!
Glad to help -- thanks for the points, ~Ray