Discussion Community

IT Acumens Forum => Programming Discussions for Engineers => Web Services in PHP => Topic started by: sukishan on Aug 23, 2009, 12:04 AM

Title: Creating a NuSOAP Client using PHP
Post by: sukishan on Aug 23, 2009, 12:04 AM
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 (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.

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 (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:

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 (http://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

<?xml version="1.0"?>
<definitions name="CurrencyExchangeService"
             targetNamespace="http://www.xmethods.net/sd/CurrencyExchangeService.wsdl (http://www.xmethods.net/sd/CurrencyExchangeService.wsdl)"
             xmlns:tns="http://www.xmethods.net/sd/CurrencyExchangeService.wsdl (http://www.xmethods.net/sd/CurrencyExchangeService.wsdl)"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema (http://www.w3.org/2001/XMLSchema)"
             xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/ (http://schemas.xmlsoap.org/wsdl/soap/)"
             xmlns="http://schemas.xmlsoap.org/wsdl/ (http://schemas.xmlsoap.org/wsdl/)">
<message name="getRateRequest">
  <part name="country1" type="xsd:string"/>
  <part name="country2" type="xsd:string"/>
<message name="getRateResponse">
  <part name="Result" type="xsd:float"/>
<portType name="CurrencyExchangePortType">
  <operation name="getRate">
   <input message="tns:getRateRequest" />
   <output message="tns:getRateResponse" />
<binding name="CurrencyExchangeBinding" type="tns:CurrencyExchangePortType">
  <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http (http://schemas.xmlsoap.org/soap/http)"/>
  <operation name="getRate">
   <soap:operation soapAction=""/>
   <input >
    <soap:body use="encoded" namespace="urn:xmethods-CurrencyExchange"
      encodingStyle="http://schemas.xmlsoap.org/soap/encoding/ (http://schemas.xmlsoap.org/soap/encoding/)"/>
    <soap:body use="encoded" namespace="urn:xmethods-CurrencyExchange"
      encodingStyle="http://schemas.xmlsoap.org/soap/encoding/ (http://schemas.xmlsoap.org/soap/encoding/)"/>
<service name="CurrencyExchangeService">
  <documentation>Returns the exchange rate between the two currencies</documentation>
  <port name="CurrencyExchangePort" binding="tns:CurrencyExchangeBinding">
   <soap:address location="http://services.xmethods.net:80/soap (http://services.xmethods.net:80/soap)"/>
Next step, our client contacts the server requesting the conversion rate using the getRate method. The parameters are sent according to the Web service specifications:

POST /soap HTTP/1.0
User-Agent: NuSOAP v0.6
Host: services.xmethods.net
Content-Type: text/xml
Content-Length: 622
SOAPAction: ""

<?xml version="1.0"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/ (http://schemas.xmlsoap.org/soap/encoding/)"
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/ (http://schemas.xmlsoap.org/soap/envelope/)"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema (http://www.w3.org/2001/XMLSchema)"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance (http://www.w3.org/2001/XMLSchema-instance)"
  xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/ (http://schemas.xmlsoap.org/soap/encoding/)"
  xmlns:si="http://soapinterop.org/xsd (http://soapinterop.org/xsd)"
<country1 xsi:type="xsd:string">usa</country1>
<country2 xsi:type="xsd:string">canada</country2>