wglenn01 Posted March 14, 2013 Share Posted March 14, 2013 Hi all, need some quick help i'm working on a cart script with Mercury Payments, their "initializePayment" system throws this XML back to me but for some reason I cannot figure out how to parse it to grab the PaymentID out of it. Any clues? I've been playing with SimpleXML for about the last 6 hours before I gave up. Thanks a ton! <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><InitializePaymentResponse xmlns="http://www.mercurypay.com/"><InitializePaymentResult><ResponseCode>0</ResponseCode><PaymentID>8f19cc14-27fc-40ec-a929-bee4f043387f</PaymentID><Message>Initialize Successful</Message></InitializePaymentResult></InitializePaymentResponse></soap:Body></soap:Envelope> William Quote Link to comment Share on other sites More sharing options...
ignace Posted March 14, 2013 Share Posted March 14, 2013 (edited) The problem is in your 3rd file, line 312 column 5. If you want something more specific you'll probably gonna need to post some more info, like the error you get and your current code. Edited March 14, 2013 by ignace Quote Link to comment Share on other sites More sharing options...
wglenn01 Posted March 14, 2013 Author Share Posted March 14, 2013 Huh? Not sure I understand? Quote Link to comment Share on other sites More sharing options...
ignace Posted March 14, 2013 Share Posted March 14, 2013 We need the error you get and your current code to be able to help you. Quote Link to comment Share on other sites More sharing options...
Maq Posted March 14, 2013 Share Posted March 14, 2013 Post what you have already. Quote Link to comment Share on other sites More sharing options...
wglenn01 Posted March 14, 2013 Author Share Posted March 14, 2013 I'm not getting any errror, everything I try (latest version below) returns nothing. I store that XML response in a variable called $returned, then my latest iteration to try and exctract the "PaymentID" is: $multi_array = json_decode( json_encode($returned) , 1); Quote Link to comment Share on other sites More sharing options...
ignace Posted March 14, 2013 Share Posted March 14, 2013 Try that: if (preg_match('!<PaymentID>(.*?)</PaymentID>!', $returned, $match)) { echo $match[1]; } Quote Link to comment Share on other sites More sharing options...
wglenn01 Posted March 14, 2013 Author Share Posted March 14, 2013 Woohoo perfect!! Didn't even think about that. Thank you so much.William Quote Link to comment Share on other sites More sharing options...
salathe Posted March 14, 2013 Share Posted March 14, 2013 The key part that you are missing is likely the use of SimpleXMLElement::children() to access elements under different namespaces. You have two namespaces to deal with; one for SOAP, and one for Mercury. The general idea is that children() selects children (weird, huh!) and you can specify the XML namespace that those children should belong to by prefix or URI. $envelope = simplexml_load_string('…'); $payment_id = (string) $envelope->children('soap', true) ->Body ->children('http://www.mercurypay.com/') ->InitializePaymentResponse ->InitializePaymentResult ->PaymentID; echo $payment_id; This will print out 8f19cc14-27fc-40ec-a929-bee4f043387f with your XML. Navigating through the XML structure like that can be long-winded: it is often a little neater to use XPath to get an array of the elements you're interested in. $envelope = simplexml_load_string('…'); $envelope->registerXPathNamespace('mercury', 'http://www.mercurypay.com/'); // xpath() returns an array, even if only one element matches $payment_ids = $envelope->xpath('descendant::mercury:PaymentID'); $payment_id = (string) $payment_ids[0]; echo $payment_id; Or, you could use a regular expression match. Helpful links SimpleXML Basic Usage XPath overview XML namespaces SimpleXMLElement::children() SimpleXMLElement::xpath() Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.