Creating a NuSOAP Client using PHP

Started by sukishan, Aug 22, 2009, 06:34 PM

Previous topic - Next topic

sukishan

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

<?xml version="1.0"?>
<definitions name="CurrencyExchangeService"
             targetNamespace="http://www.xmethods.net/sd/CurrencyExchangeService.wsdl"
             xmlns:tns="http://www.xmethods.net/sd/CurrencyExchangeService.wsdl"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema"
             xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
             xmlns="http://schemas.xmlsoap.org/wsdl/">
<message name="getRateRequest">
  <part name="country1" type="xsd:string"/>
  <part name="country2" type="xsd:string"/>
</message>
<message name="getRateResponse">
  <part name="Result" type="xsd:float"/>
</message>
<portType name="CurrencyExchangePortType">
  <operation name="getRate">
   <input message="tns:getRateRequest" />
   <output message="tns:getRateResponse" />
  </operation>
</portType>
<binding name="CurrencyExchangeBinding" type="tns:CurrencyExchangePortType">
  <soap:binding style="rpc" transport="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/"/>
   </input>
   <output>
    <soap:body use="encoded" namespace="urn:xmethods-CurrencyExchange"
      encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
   </output>
  </operation>
</binding>
<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"/>
  </port>
</service>
</definitions>
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/"
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
  xmlns:si="http://soapinterop.org/xsd"
  xmlns:galactivism="urn:xmethods-CurrencyExchange">
<SOAP-ENV:Body>
<galactivism:getRate>
<country1 xsi:type="xsd:string">usa</country1>
<country2 xsi:type="xsd:string">canada</country2>
</galactivism:getRate>
</SOAP-ENV:Body>
A good beginning makes a good ending

Quick Reply

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it has been approved by a moderator.

Name:
Email:
Verification:
Please leave this box empty:
Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

Shortcuts: ALT+S post or ALT+P preview