Consuming Web Services with PHP and SOAP

Ivan Kip
2 min readNov 30, 2016
Simple soap client implementation in PHP

I had a challenge of implementing integration with a SOAP service using PHP scripting language and have never had touched SOAP with PHP. There doesn’t appear to be any straight forward tutorials about how to talk to a soap service using PHP. There are many available programming languages that support web services. PHP is one such language, with a powerful guns of open source functions and tools. So I set out to find out how to implement client side application to consume a SOAP API. SOAP is the bread and butter of corporate web service developers. It has been deeply integrated into Microsoft .NET and IBM WebSphere

If you’re like me, the approach that I take to get started is see one simple example, explaining the parts and how they fit. Once I see how something works, start to finish, I am ready to go. There are several libraries available that helps developer to build both Web service clients and servers. Nusoap is one such. One of the great features of NuSOAP is the built-in WSDL support. Installing the API is a snap: all you need is a PHP enabled server. The required libraries are contained in a file called nusoap.php

Creating a NuSOAP Client using PHP

Creating a SOAP client with the library is very simple. SOAP’s powers lies in its use of namespaces, XML Schema datatypes, and its flexibility with regard to transports. But SOAP can make live hard with the fact that the specs and implementation is more complex compared to REST API. The challenge with SOAP is retrieving data from the Response object returned from endpoint.New object is created with the PHP5 function SoapClient(). When first testing a service it’s best to set trace and exceptions to true so that any errors can be discovered more easily. I recommend that you wrap the request in a try/catch pair to prevent any fatal errors.

The return data is supplied in an array rather than XML like the service expects. To access values of each node you need to convert it to xml string using simpleXML extension of PHP which parses the response and make it easy to access data with nodes or using indices. If the response has nested elements then we would need to loop through to match. Also, you can dump the response with println() or var_dump() and inspect the response in detail.

Now almost 2 weeks later, I look at all the Lynda and Knp University tutorials as a invaluable learning experience.

--

--