Home > Backend Development > PHP Tutorial > How to Retrieve the \'payment\' Element from a SOAP XML Response Using PHP?

How to Retrieve the \'payment\' Element from a SOAP XML Response Using PHP?

DDD
Release: 2024-12-07 02:56:12
Original
451 people have browsed it

How to Retrieve the 'payment' Element from a SOAP XML Response Using PHP?

Parsing SOAP XML

Issue: Retrieving 'payment' Element

Problem Overview:

PHP's simplexml_load_string() fails to parse a SOAP XML response and retrieve the 'payment' element.

Solution: Stripping Namespace Prefixes

One effective method to address this issue involves removing namespace prefixes from the XML response before using simplexml_load_string(). Here's how it's done:

$your_xml_response = '<SOAP XML here>';
$clean_xml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $your_xml_response);
$xml = simplexml_load_string($clean_xml);
Copy after login

Example: Parsing SOAP Response

For the provided SOAP XML response:

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <PaymentNotification xmlns="http://apilistener.envoyservices.com">
      <payment>
        <uniqueReference>ESDEUR11039872</uniqueReference>
        <epacsReference>74348dc0-cbf0-df11-b725-001ec9e61285</epacsReference>
        <postingDate>2010-11-15T15:19:45</postingDate>
        <bankCurrency>EUR</bankCurrency>
        <bankAmount>1.00</bankAmount>
        <appliedCurrency>EUR</appliedCurrency>
        <appliedAmount>1.00</appliedAmount>
        <countryCode>ES</countryCode>
        <bankInformation>Sean Wood</bankInformation>
        <merchantReference>ESDEUR11039872</merchantReference>
      </payment>
    </PaymentNotification>
  </soap:Body>
</soap:Envelope>
Copy after login

Executing the code:

$your_xml_response = '<!-- Your SOAP XML -->';
$clean_xml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $your_xml_response);
$xml = simplexml_load_string($clean_xml);

foreach ($xml->Body->PaymentNotification->payment as $item) {
    print_r($item);
}
Copy after login

This will output the 'payment' element as a SimpleXMLElement object, providing access to its child elements.

The above is the detailed content of How to Retrieve the \'payment\' Element from a SOAP XML Response Using PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template