PHP Extracting the value from a SOAP response

42 views Asked by At

I have the following PHP code. The code will send a SOAP request and get a SOAP response. I need to get the value between 2 tags. When I run it. I will get the following response from the server on Chrome.

Code

$xml_post_string='<soapenv:Envelope 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.allianz-assistance.com/emagin/services/PaymentService/1.0">
                <soapenv:Header/>
                <soapenv:Body>
                    <tns:authorize version="1.0" xmlns:tns="http://www.allianz-assistance.com/emagin/services/PaymentService/1.0" xmlns:emagin="http://www.mondial-assistance.com/ecommerce/schema/">
                    <tns:globalRequestId>paymentOrder</tns:globalRequestId>
                    <tns:authentication>
                        <tns:userId>*****</tns:userId>
                        <tns:password>*******</tns:password>
                    </tns:authentication>
                    <tns:accountId>ACCOUNT_AWC_TT_MOTO</tns:accountId>
                    <tns:customerInformation>
                        <tns:partnerCode>ALS</tns:partnerCode>
                        <tns:buCode>CH</tns:buCode>
                    </tns:customerInformation>
                    <tns:paymentInformation>
                        <tns:paymentHolder>
                            <tns:name>Charlie</tns:name>
                            <tns:email>[email protected]</tns:email>
                            <tns:address>
                                <tns:address>11/22 Street1 Road1</tns:address>
                                <tns:city>Penang</tns:city>
                                <tns:country>MY</tns:country>
                            </tns:address>
                        </tns:paymentHolder>
                        <tns:amount currency="USD">33.33</tns:amount>
                        <tns:cardInformation>
                            <tns:cardType>VISA</tns:cardType>
                            <tns:cardNumber>*************</tns:cardNumber>
                            <tns:cvc>*****</tns:cvc>
                            <tns:expiryDate month="**" year="*****"/>
                        </tns:cardInformation>
                        <tns:shopperInformation>
                            <tns:browser>
                                <tns:userAgentHeader>Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36</tns:userAgentHeader>
                                <tns:acceptHeader>application/json, text/plain, */*</tns:acceptHeader>
                            </tns:browser>
                            <tns:ipAddress>103.233.174.2</tns:ipAddress>
                            <tns:sessionId>1</tns:sessionId>
                        </tns:shopperInformation>
                    </tns:paymentInformation>
                    </tns:authorize>
                </soapenv:Body>
            </soapenv:Envelope>';

$headers = array(
    "Content-Type: text/xml; charset=utf-8",

    "Content-length: ".strlen($xml_post_string),
    "SOAPAction: http://www.allianz-assistance.com/emagin/services/PaymentService/1.0/authorize"
);

$url = 'https://onepay-services.uat.aws.emagin.eu/api/soap/payment';


$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);

echo ($response);

curl_close($ch);

$res = htmlentities(strval($response));

var_dump($res);

The response I get back at Chrome

paymentOrder2024-02 21T09:36:07.796ZAWC_TTACCOUNT_AWC_TT_MOTOAuthorizePaymentCardALSCHTX00VMR1HN5GBF5SSuccessRD00VMR1HN5GBF5SORDER_APPROVEDAUTHORIZEDAWCTALENTTRUSTMOTOTX00VMR1HN5GBF5SPR00VMR1HN5GBF5S (CVC status: )
string(1759) "<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ns2:paymentServiceResponse xmlns:ns2="http://www.allianz-assistance.com/emagin/services/PaymentService/1.0" xmlns:ns3="http://www.allianz-assistance.com/emagin/services/PaymentService/common/1.0" version="1.0"><ns2:globalRequestId>paymentOrder</ns2:globalRequestId><ns2:date>2024-02-21T09:36:07.796Z</ns2:date><ns2:userId>AWC_TT</ns2:userId><ns2:accountId>ACCOUNT_AWC_TT_MOTO</ns2:accountId><ns2:paymentOperation>Authorize</ns2:paymentOperation><ns2:paymentMethod>PaymentCard</ns2:paymentMethod><ns2:customerInformation><ns2:partnerCode>ALS</ns2:partnerCode><ns2:buCode>CH</ns2:buCode></ns2:customerInformation><ns2:paymentStatus><ns2:paymentTransactionId>TX00VMR1HN5GBF5S</ns2:paymentTransactionId><ns2:orderStatus>Success</ns2:orderStatus><ns2:orderId>RD00VMR1HN5GBF5S</ns2:orderId><ns2:orderReason>ORDER_APPROVED</ns2:orderReason><ns2:paymentTransactionStatus>AUTHORIZED</ns2:paymentTransactionStatus><ns2:providerPaymentStatus code="WorldPayDXML"><ns2:providerAccountId>AWCTALENTTRUSTMOTO</ns2:providerAccountId><ns2:providerTransactionId>TX00VMR1HN5GBF5S</ns2:providerTransactionId><ns2:providerOrderId>PR00VMR1HN5GBF5S</ns2:providerOrderId><ns2:providerReason> (CVC status: )</ns2:providerReason></ns2:providerPaymentStatus></ns2:paymentStatus></ns2:paymentServiceResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>"

In pic

Question

How do I get the value between <ns2:paymentTransactionId>TX00VMR1HN5GBF5S</ns2:paymentTransactionId>

Solution

$xml = simplexml_load_string(htmlspecialchars_decode($res));

$xml->registerXPathNamespace('ns2', 'http://www.allianz-assistance.com/emagin/services/PaymentService/1.0');

foreach ($xml->xpath('//ns2:paymentTransactionId') as $item)
{
    echo '<pre> The value is: ' . $item . '</pre>';
}

Managed to solve it. It requires htmlspecialchars_decode to handle <

0

There are 0 answers