I’ve been doing some SOAP work lately (communicating with Java services by way of XML) and it’s time to recap how unbelievably hassle free and simple this is with the right tools.
What this example/short tutorial is not covering is setting up your own server, generating the WSDL (Web Services Description Language) XML and all, here we just get info from such a server and use it to access the information we want. Check the manual for more info on how to setup your own server.
The idea is that our client will access a WSDL file on the server which describes available services and which parameters they accept etc. Our client will then expose this natively, in our PHP case that should be as simple as calling $obj->remoteFunction(array(‘key’ => ‘value’)). Also note the native array argument there.
In my case the WSDL looks very similar to this:
. . . <s:element name="Warehouse_Function"> <s:complexType> <s:sequence> <s:element minOccurs="0" maxOccurs="1" name="username" type="s:string" /> <s:element minOccurs="0" maxOccurs="1" name="password" type="s:string" /> <s:element minOccurs="0" maxOccurs="1" name="array" type="tns:ArrayOfInt" /> </s:sequence> </s:complexType> </s:element> <s:complexType name="ArrayOfInt"> <s:sequence> <s:element minOccurs="0" maxOccurs="unbounded" name="int" type="s:int" /> </s:sequence> </s:complexType> <s:element name="Warehouse_FunctionResponse"> <s:complexType> <s:sequence> <s:element minOccurs="0" maxOccurs="1" name="Warehouse_FunctionResult" type="s:string" /> </s:sequence> </s:complexType> </s:element> . . .
Note how the array is handled here, explicitly specified and what it should contain (integers in our case).
When Zend_Soap_Client has access to the above information, calling the service becomes as easy as this:
include_once('Zend/Loader.php'); Zend_Loader::loadClass('Zend_Soap_Client'); $client = new Zend_Soap_Client("url to wsdl", array('encoding' => 'UTF-8')); $result = $client->Warehouse_Function( array('username' => 'henrik', 'password' => 'some_password', 'array' => array(1,2)) ); echo $result->Warehouse_FunctionResult;
We need the URL to the WSDL file of course, in my case we also need to set the encoding to UTF-8 (if I remember correctly it’s UTF-8 per default but I set it explicitly anyway, just to be sure).
Note how the WSDL info corresponds to what we pass to Warehouse_Function.
Finally if everything went OK we have the result info in the Warehouse_FunctionResult variable of the result object.