Working SOAP services, with PHP, can be a cumbersome task if you’ve never worked on one. It happened to me. I had a project to estimate for a Shipping service using SOAP.

Tutorials around it were never showing me the information I needed, especially for using the correct namespaces, nested nodes, and such. Collecting information from everywhere and combining the knowledge I got with a bit of research, I was able to form the XML their SOAP service needed.

Please understand that I am not an expert in building complex XMLs for SOAP requests and I might be wrong when explaining some parts. But I am writing this from my experience and this worked for me.

This is the XML we’ll build in this tutorial:

When building such XML for the SoapClient, we need to build it from the inside out.

So we need to create data that we’ll insert in the, for example, PackageList node. Then the whole object that contains that data, needs to be added to the ShipmentOrder node.

Then the object with data for ShipmentOrder needs to be added to the shipmentOrders node and so on.

It is also important to understand that when we have an object and add a public attribute, that attribute name will be used for node name, and the value for node value.

Let’s start 🙂

Building the Authentication Header for SoapClient

This will be added to our SoapClient later. We’re constructing a node AuthentificationHeader that will have the nodes UserName and Password.

We’re also setting a custom namespace Your SOAP service might require a different one.

Creating the SoapClient

We’ll create the SoapClient and also set the header that we’ve created above.

Initial Data and How to call the SOAP service

Here is now the initial data that we’ll pass and also an example of how to call the SOAP Service.

All the code we write further would need to go between those 2 parts.

We’re calling their service CreateShipmentOrders (your SOAP will have different ones) and we’re sending the first node of SOAP Body that is named the same CreateShipmentOrders. In there we’re sending the data that we’ll construct.

The first data, which you can see in the XML example at the bottom, are username and password. Since they’re the first children to the node, it will have the same namespace (at least in my experience).

Other sub-nodes might not have any namespace or different ones if not set specifically (especially those with values). We’ll fix that in the code as well when defining such values.

Creating the SoapClient Order data

We have the children username & password passed. The missing node is shipmentOrders. That node has another child node, and that one is named ShipmentOrder. We need to build that one before passing it to the $params.

We’re now constructing the object to hold data for the node ShipmentOrder. I named it the same as node so I know where we’re adding data.

On that empty object, we’re adding the order info. We’re adding the nodes with values set with SoapVar. With that, we can define specifically what namespace needs to be set.

We are also setting the type of value as XSD_STRING.

I don’t have a lot of knowledge here as I was constructing the nodes and values with the “trial-and-error” method. If you have various types of data and this doesn’t work for you, try different ones. You can find them on the Soap Constants page.

But, we can’t yet add this object to an object that will represent shipmentOrders node. We still have data to add there.

Adding SoapClient sub-nodes to Order data

First, let’s add the package data that will be under the PackageList node.

We’re first creating an empty object to hold data for the node Package. To add an empty node there, named Barcode, the string type wouldn’t work as it would create <Barcode></Barcode>.

Service would throw errors, so I put it as an object and it worked.

After we’ve built that object with Package data, we added it to another object as the attribute Package. Then such an object is being added to our order object as PackageList to create the node PackageList in our XML example.

Let’s now add the AdditionalServiceList node.

Using the same knowledge we got when constructing the PackageList, we’re doing the same here to construct the node AdditionalServiceList.

Adding Order data to shipmentOrders node

Now, it’s time to add such object with all the order data under the shipmentOrders node.

First, we create an empty object to set the attribute ShipmentOrder. This will then be set as the node ShipmentOrder.

Then, we add that object to the variable $params. We set it as shipmentOrders so it creates the node with the same name.

That’s it, we have the whole XML constructed and the SOAP service returns a result.

Reading Results with PHP SoapClient

To read the data, we can put the response from the service inside of an object. Example of the class:

Of course, this class uses data that the SOAP service that I worked with returned. It will be different for your SOAP services, but this can help understand it.


Working with the PHP SoapClient, for those that don’t know, can be really hard. I mostly learned from sites such as StackOverflow where people discussed how to construct such XML.

The approach I took here worked for my example and it should probably work for most.

As with any other stuff when coding, you have to try it out, note what works and what doesn’t. There will be a lot of failed tries until you get a successful one. And that’s normal.

Become a Sponsor

Posted by Igor Benic

Web Developer who mainly uses WordPress for projects. Working on various project through Codeable & Toptal. Author of several ebooks at

Leave a reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.