SOAP interface – Part 2

In the first article I showed you how to SOAP works and how to define a SOAP client with PHP. Now it is time to get actual data. For this there are some different ways and it is hard to find the right one.

Functions and Types

If you don’t know the interface, the first step is to get all functions and types that are accepted from the interface. You can do this with this code:

The SOAP client offers you two functions. __getFunctions() returns all functions in an array and __getTypes() returns all types in one array. The functions are very important. You can only call this functions from this interface. There are not more things that are accepted by the interface.

Call functions

Now assume, that the call for functions gets the following result:

It is obvious that we can only get a list of products from the interface. The works as follows: We create a ProductlistRequest. Then we send this object to the interface. And then we get a ProductlistResponse. Whats that? If you look at the WSDL of the interface, you can find the definition of the ProductlistResponse object. If you check ProductlistRequest (for our example) you get to know the definition for a correct request object. We want to create this XML structure and use our data with it.

__soapCall

With this function you can call a function with the SOAP client object. The first parameter is the name of the function, the second one our request object:

The result of this is a response object. For our test we want to print it.

Named function

There is an alternative version. Here you can call a function directly by its name. For the above example this should look as follows:

Create the request object

We need a request object to pass it as parameter with the function call. This object is sent to the web service. Such a request object is only a XML object, which is defined by the SOAP interface. There are different options to create a request object.

Array request

The most simple way is to create a simple array. It is very important to do a correct nesting with the same hierarchy like the XML definition. Here is an example:

  • for __soapCall
  • for function calls with function name

As you can see, if you use __soapCall you need an extra level for a named array ‘parameters’. In our example from a web shop we want all products from the category ‘Lingerie’ with the size ‘M’.

stdClass

A more clear option is the use of a stdClass hierarchy. There you have less brackets it is easier to read if you have very complex structures. The example with stdClass looks as follows:

I hope this article is useful for you. If so, please add a comment.

Part 1

(Visited 241 times, 1 visits today)

2 Responses

  1. Stefan says:

    Nice article, but how do I know the structure of request object? Can I get somewhere the levels of nesting, objects and parameters needed to pass to objects to assemble request object?

    • You can get the structure of a request object from web service WSDL file. Sometimes it is hard to find the correct hierarchy. Most of the time it is more try and error… For my projects this is always time consuming.

Leave a Reply

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