Home > Backend Development > PHP Tutorial > How to Correctly Parse SOAP XML Responses with Namespaces in PHP Using simpleXML?

How to Correctly Parse SOAP XML Responses with Namespaces in PHP Using simpleXML?

Linda Hamilton
Release: 2024-12-05 05:08:10
Original
747 people have browsed it

How to Correctly Parse SOAP XML Responses with Namespaces in PHP Using simpleXML?

Parsing SOAP XML

Parsing XML with namespaces can sometimes cause confusion, especially when using simpleXML.

Problem Statement

The given SOAP XML response contains a namespace prefix for the soap elements. A code snippet in PHP is provided to parse the XML but returns an empty result.

Correct Parsing Approach

One simple solution is to strip the namespace prefixes from the XML response before passing it to simpleXML:

$clean_xml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $soap_response);
$xml = simplexml_load_string($clean_xml);
Copy after login

Result

Using this code, you will get the following result:

SimpleXMLElement Object
(
    [Body] => SimpleXMLElement Object
        (
            [PaymentNotification] => SimpleXMLElement Object
                (
                    [payment] => SimpleXMLElement Object
                        (
                            [uniqueReference] => ESDEUR11039872
                            [epacsReference] => 74348dc0-cbf0-df11-b725-001ec9e61285
                            [postingDate] => 2010-11-15T15:19:45
                            [bankCurrency] => EUR
                            [bankAmount] => 1.00
                            [appliedCurrency] => EUR
                            [appliedAmount] => 1.00
                            [countryCode] => ES
                            [bankInformation] => Sean Wood
                            [merchantReference] => ESDEUR11039872
                        )

                )

        )

)
Copy after login

This will allow you to access the payment element and its child elements as expected.

The above is the detailed content of How to Correctly Parse SOAP XML Responses with Namespaces in PHP Using simpleXML?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template