Here at LeaseWeb, we work a lot with SOAP web-services to integrate our internal applications with each other. Especially during development and testing of our applications as we need the ability to practice with SOAP API’s.
If you are into fancy GUI applications then maybe SoapUI is something you could use to familiarise yourself with the API you are integrating with.
If you like simplicity and the command line, or dislike heavy GUI applications you can give
php soap client a try.
php soap client is a command line application written in PHP and packed using the popular .phar format. Once installed you can start exploring soap web-services.
There are two ways of installing the client. To get the latest stable version download
soap_client.phar from here or do:
$ curl -sS http://leaseweb.github.io/php-soap-client/installer | php
This will download the
phar file to the current working directory and make it executable so you can use start using it right away by invoking:
To install the latest
master version you can get the source code directly from GitHub, package your own
.phar file and install it — using GNU Make.
In order to be able to create the
.phar file you need to have composer installed. To read more about composer refer to their excellent documentation here.
# Install php soap client $ git clone https://github.com/LeaseWeb/php-soap-client.git $ cd php-soap-client $ composer.phar install $ make $ sudo make install
If you are getting a
Failed to compile phar exception while running
make you need to set
phar.readonly = Off in your
php.ini. On a development machine this is fine to do but please be ware of the security risks when setting
make install command will install the
soap_client application to
/usr/local/bin and make it executable so you can easily call it like this:
$ soap_client php-soap-client version 2.1.3 Usage: [options] command [arguments] Options: ... Available commands: call Call the remote service with the `method` specified and output the reponse to stdout. help Displays help for a command list Lists commands list-methods Get a list of available methods to call on the remote. request Generate an xml formatted SOAP request for the given method and output to stdout. wsdl Get the WSDL of a soap service.
From this point onwards we assume you have installed the
soap_client.phar on your system in
/usr/local/bin/soap_client and that the directory
/urs/local/bin is in your
Lets say we would like to see what methods are available on the remote service
http://www.webservicex.net/ConvertTemperature.asmx. We could issue the following command:
$ soap_client --endpoint='http://www.webservicex.net/ConvertTemperature.asmx?WSDL' list-methods
Which will output the following:
If you run the above command with the
-vvv option you will get more verbose output.
In this case the only available method is
ConvertTemp. Let’s see how a SOAP XML request looks like for this method:
$ soap_client --endpoint='http://www.webservicex.net/ConvertTemperature.asmx?WSDL' request ConvertTemp <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.webserviceX.NET/"> <SOAP-ENV:Body> <ns1:ConvertTemp> <ns1:Temperature>0</ns1:Temperature> <ns1:FromUnit></ns1:FromUnit> <ns1:ToUnit></ns1:ToUnit> </ns1:ConvertTemp> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
If you want to make a SOAP request to the
ConvertTemp method on the remote service use the
call sub command:
$ soap_client --endpoint='http://www.webservicex.net/ConvertTemperature.asmx?WSDL' call --editor ConvertTemp
--editor option after the
call sub command. If you use the
soap_client will open up the editor specified in your environment variable
$EDITOR so you are able to modify the request XML before sending it.
If you issue the same request multiple times, you could save a soap request as a local XML file and pass it to
/dev/stdin of the
soap_client call command:
# Get the request xml and store it locally $ soap_client --endpoint='http://www.webservicex.net/ConvertTemperature.asmx?WSDL' request ConvertTemp > my_sample_request.xml # Now edit my_sample_request.xml # Now you can call the ConvertTemp method with this pre-prepared request $ soap_client --endpoint='http://www.webservicex.net/ConvertTemperature.asmx?WSDL' call ConvertTemp < my_sample_request.xml
Since you will be repeating
soap_client commands frequently in a short time while exploring a remote web service you can save yourself some time by setting an environment variable
SOAPCLIENT_ENDPOINT that contains the URL to the WSDL. When this environment variable is set you can omit the
--endpoint command line option. Let’s do this now and call the
$ export SOAPCLIENT_ENDPOINT='http://www.webservicex.net/ConvertTemperature.asmx?WSDL' $ soap_client call ConvertTemp < my_sample_request.xml
I wanted to know how much 107,6 degrees Fahrenheit is in Celsius, so my
$ cat my_sample_request.xml <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.webserviceX.NET/"> <SOAP-ENV:Body> <ns1:ConvertTemp> <ns1:Temperature>107.6</ns1:Temperature> <ns1:FromUnit>degreeFahrenheit</ns1:FromUnit> <ns1:ToUnit>degreeCelsius</ns1:ToUnit> </ns1:ConvertTemp> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
And the result:
$ soap_client call ConvertTemp < my_sample_request.xml stdClass Object ( [ConvertTempResult] => 42 )
The answer is 42.
If you rather see the responses in XML format you can use the
--xml command line option:
$ soap_client call --xml ConvertTemp < my_sample_request.xml <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <ConvertTempResponse xmlns="http://www.webserviceX.NET/"> <ConvertTempResult>42</ConvertTempResult> </ConvertTempResponse> </soap:Body> </soap:Envelope>
This tutorial should give you enough information to get started with exploring, testing and/or developing SOAP API’s.
In a future blog post, I will continue the topic of the
php soap client. We are currently working on packing the .phar archive for the web.
So you would be able to drop the
soap_client.phar somewhere in your apache
DocumentRoot and explore SOAP services from your browser.