0

I have the following xml file:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
 <soap:Body>
  <ns2:doTransactionResponse xmlns:ns2="http://soap.api.controller.web.payjar.com/">
   <return>
    <displayMessage>An error occurred with this payment, please contact your merchant (ref: P022)</displayMessage>
    <merchantReference>mercRef_1350047403</merchantReference>
    <payUReference>11999149347</payUReference>
    <pointOfFailure>PAYU</pointOfFailure>
    <resultCode>P022</resultCode>
    <resultMessage>Transaction is not in the correct state - last transaction: FINALIZE state: SUCCESSFUL</resultMessage>
    <successful>false</successful>
   </return>
  </ns2:doTransactionResponse>
 </soap:Body>
</soap:Envelope>

I need to test the result code in the xml file against a series of codes. How would I get the specific element value of <resultCode> using PHP?

heinkasner
  • 425
  • 5
  • 18

3 Answers3

3

DOMDocument in combination with getElementsByTagName would be a quick solution.

DOMDocument::getElementsByTagName

$xml_string = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
 <soap:Body>
  <ns2:doTransactionResponse xmlns:ns2="http://soap.api.controller.web.payjar.com/">
   <return>
    <displayMessage>An error occurred with this payment, please contact your merchant (ref: P022)</displayMessage>
    <merchantReference>mercRef_1350047403</merchantReference>
    <payUReference>11999149347</payUReference>
    <pointOfFailure>PAYU</pointOfFailure>
    <resultCode>P022</resultCode>
    <resultMessage>Transaction is not in the correct state - last transaction: FINALIZE state: SUCCESSFUL</resultMessage>
    <successful>false</successful>
   </return>
  </ns2:doTransactionResponse>
 </soap:Body>
</soap:Envelope>';



$dom = new DOMDocument;
$dom->loadXML($xml_string);
$nodes = $dom->getElementsByTagName('resultCode');
foreach ($nodes as $node) {
    echo $node->nodeValue;
}

Result: P022

Of course there are several other ways to work with XMLs in PHP (xpath etc.)

MAQU
  • 562
  • 2
  • 12
2

Using xpath. Here's the code as mentioned by KURN,

<?php
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <ns2:doTransactionResponse
            xmlns:ns2="http://soap.api.controller.web.payjar.com/">
            <return>
                <displayMessage>An error occurred with this payment, please contact
                    your merchant (ref: P022)</displayMessage>
                <merchantReference>mercRef_1350047403</merchantReference>
                <payUReference>11999149347</payUReference>
                <pointOfFailure>PAYU</pointOfFailure>
                <resultCode>P022</resultCode>
                <resultMessage>Transaction is not in the correct state - last
                    transaction: FINALIZE state: SUCCESSFUL</resultMessage>
                <successful>false</successful>
            </return>
        </ns2:doTransactionResponse>
    </soap:Body>
</soap:Envelope>
';

$xml = simplexml_load_string($xml, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");
$xml->registerXPathNamespace('soapenv', 'http://schemas.xmlsoap.org/soap/envelope/');

foreach($xml->xpath('//soapenv:Body') as $header)
{   
    $arr = $header->xpath('//resultCode'); // Should output 'something'.
    $resultCode = $arr[0];
    echo $resultCode;// outputs P022
}
?>
Dushyant Joshi
  • 3,672
  • 3
  • 28
  • 52
1

Actually this is quite easy with PHP:

$resultCode = simplexml_load_string($xml_string)->xpath('//resultCode')[0];

var_dump((string) $resultCode); // string(4) "P022"

Note that this code has no error checking, it might fatal-error in case the XML can not be loaded and warnings/notices if the element you're looking for does not exist at least once.

hakre
  • 193,403
  • 52
  • 435
  • 836