Creating a NuSOAP Client using PHP Creating a SOAP client with the library is very simple. To illustrate how the SOAP client works, I've decided to call a web service from XMethods.net (http://www.xmethods.net). This website has a wide range of useful web services you can access freely. In our particular example, we will be querying the currency converter Web service to find out the exchange rate between Canadian and American dollars.
Here is a breakdown of the PHP code needed to access the currency converter. First, we need to bring in the library file. The require_once command will make sure the functions are only added once into our script.
<? require_once('nusoap.php'); Next step we will define where the WSDL is located and create an instance of the soapclient class to access the web service. WSDL stands for Web Services Description Language, an XML file which describes the interface of a web service:
$wsdl="http://www.xmethods.net/sd/2001/CurrencyExchangeService.wsdl"; $client=new soapclient($wsdl, 'wsdl'); We then send in two parameters through the SOAP client, the two countries that we want to compare to get the currency exchange rate. We then call the getRate function and pass our parameters to get a response from the remote server. The result is then displayed to the user:
$param=array( 'country1'=>'usa', 'country2'=>'canada' ); echo $client->call('getRate', $param); ?> To help better explain the process happening during SOAP communication, here is a sample of packet data transmitted from client to server and vice versa. First, our web client requests the WSDL file from the XMethods server:
GET /sd/2001/CurrencyExchangeService.wsdl HTTP/1.0 Host: www.xmethods.net The WSDL file is then passed into our SOAP client. It contains definitions for all the methods available from the CurrencyExchangeService Web service:
HTTP/1.1 200 OK Date: Tue, 10 Feb 2004 11:45:24 GMT Server: Apache/1.3.26 (Unix) Enhydra-Director/3 PHP/4.0.6 DAV/1.0.3 AuthNuSphere/1.0.0 Last-Modified: Tue, 23 Oct 2001 18:10:37 GMT ETag: "1981a8-654-3bd5b29d" Accept-Ranges: bytes Content-Length: 1620 Connection: close Content-Type: text/xml