SOAP, originally defined as Simple Object Access protocol, is a protocol specification for exchanging structured information in the implementation of web services in computer networks. It relies on XML Information Set for its message format, and usually relies on other application layer protocols, most notably Hypertext Transfer Protocol (HTTP) or Simple Mail Transfer Protocol (SMTP), for message negotiation and transmission.
I will talk only about part of SOAP and that is how to handle SOAP response using PHP.
So let’s say you just made a SOAP request and you got response that looks something like this
You may think hey it’s easy I just need to use simplexml_load_string or simplxml_load_file and that’s it.
If you were to do
You’d get empty SimpleXML object.
The reason why it didn’t work is because you need to register namespaces. If you were to look again at that XML you’d notice that there are ‘soap’ and ‘tns’ prefixes. To successfully read our XML we need to register those namespaces.
Now if you were to var_dump it you’d get all needed data.
The second parameter in children method that is set to true tells it that first parameter is a prefix. We could have also used it like this
And if would have worked as well.
If you have a better way to handle SOAP responses comment it down below.
This post is on GitHub. If you spot any errors please do a pull request.
comments powered by Disqus