A quick example of how easy it is to consume web services with PHP, using PEAR::SOAP and WSDL, as there isn’t much material around.
Picking a web service at random described by this WSDL document http://wavendon.dsdata.co.uk/axis/services/CarRentalQuotes?wsdl, here’s how to consume it with PEAR::SOAP.
First check out the proxy code generated from the WSDL document so you understand the available methods (note this a good way to mentally “reverse engineer” what WSDL actually says);
<?php
require_once('SOAP/Client.php');
$wsdl=new SOAP_WSDL(
'http://wavendon.dsdata.co.uk/axis/services/CarRentalQuotes?wsdl');
// Look at the generated code...
echo ( $wsdl->generateProxyCode() );
?>
This will display something like;
class WebService_CarRentalQuotesService_CarRentalQuotes extends SOAP_Client { function WebService_CarRentalQuotesService_CarRentalQuotes() { $this->SOAP_Client( "http://wavendon.dsdata.co.uk/axis/services/CarRentalQuotes", 0); } function getQuotes($carType, $country, $currency, $pickupDate, $pickupLocation, $returnDate, $returnLocation) { // SOAP Stuff here } function getCountries() { // SOAP Stuff here } function getLocations($country) { // SOAP Stuff here } function getCurrencies() { // SOAP Stuff here } function getCarTypes() { // SOAP Stuff here } }
Now you know what the methods are and their parameters.
Note I’ve chopped down the above code to make it readable but make sure you try it yourself - basically the generated code makes use of the underlying SOAP_Client class - if you’ve had any experience of XML-RPC, you had to hand code this stuff - this is the big step forward WSDL brings.
You could then apply eval() to the above code then instantiate an object by PEAR::SOAP already provides the method getProxy() which does it for you;
<?php
require_once('SOAP/Client.php');
$wsdl=new SOAP_WSDL(
'http://wavendon.dsdata.co.uk/axis/services/CarRentalQuotes?wsdl');
// Create an object directly from the proxy code
$carRentalQuotes=$wsdl->getProxy();
$countries=$carRentalQuotes->getCountries();
print_r ( $countries );
?>
You get back a list of countries from the remote SOAP server, which you could then feed back into the getLocations() method. Note bad for four lines of code.
Note: at some point people will start implement WSCI (Web Service Choreography InterfaceXMethods - watch out for those described as “Document Literal” - these are a bit more tricky - reasons explained in brief here.
I’ll look at building SOAP servers some other time with PEAR::SOAP (it’s also easy - credit to the authors). The harder part is writing WSDL document to describe your service - basically you need to do it manually. Explaining WSDL is more than a quick article but some time...
Follow On
Check out PEAR::SOAP Server Quick Start to find out about build SOAP servers and hand coding SOAP clients (Quick but not fast...)