mavmanau
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><GetPay mentsRespo nse xmlns="https://somedomain/"><GetPaymentsResul t xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Data><Payment> <BankFaile dReason/>< BankReceip tID>362021 </BankRece iptID><Ban kReturnCod e>O</BankR eturnCode> <CustomerN ame>JG testing</CustomerName><Deb itDate>201 2-08-24T00 :00:00</De bitDate><E zidebitCus tomerID/>< InvoiceID> 0</Invoice ID><Paymen tAmount>10 0</Payment Amount><Pa ymentID>WE B51409</Pa ymentID><P aymentMeth od>CR</Pay mentMethod ><PaymentR eference>J G123</Paym entReferen ce><Paymen tSource>WE B</Payment Source><Pa ymentStatu s>P</Payme ntStatus>< ScheduledA mount>97.4 2</Schedul edAmount>< Settlement Date i:nil="true"/><Transaction FeeClient> 0</Transac tionFeeCli ent><Trans actionFeeC ustomer>2. 58</Transa ctionFeeCu stomer><Tr ansactionT ime>2012-0 8-25T10:38 :00</Trans actionTime ><YourGene ralReferen ce/><YourS ystemRefer ence/></Pa yment></Da ta><Error> 0</Error>< ErrorMessa ge i:nil="true"/></GetPayment sResult></ GetPayment sResponse> </s:Body>< /s:Envelop e>
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!
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><GetPay
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!
Please use the code snippet feature to post the XML strings - it is much easier to read that way.
ASKER
hi,
sorry, how is that?
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>
ASKER
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);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
many thanks for your assistance!! So quick as well!!
Glad to help -- thanks for the points, ~Ray