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:
echo "<h3>Functions</h3>"; $functions = $client->__getFunctions(); foreach($functions as $d){ echo "</br>".$d; } echo "</br><h3>Types</h3>"; $types = $client->__getTypes(); foreach($types as $t) { echo "</br>".$t; }
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:
ProductlistResponse getProductlist(ProductlistRequest $body)
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:
$response = $client->__soapCall("getProduktliste", $request); var_dump($response);
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:
$response = $client->getProduktliste($request); var_dump($response);
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
$request = array( 'parameters' => array( 'ProductlistRequest' => array( 'Category' => 'Lingerie', 'Size' => 'M' ), ), );
- for function calls with function name
$request = array( 'ProductlistRequest' => array( 'Category' => 'Lingerie', 'Size' => 'M' ), );
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:
$request = new stdClass(); $request->ProductlistRequest = new stdClass(); $request->ProductlistRequest->Category = 'Lingerie'; $request->ProductlistRequest->Size = 'M';
I hope this article is useful for you. If so, please add a comment.
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.