SOAP in PHP with NUSOAP

Here we will write a simple web service which will take a [NAME] as a input parameter
and reply greeting the person with "Hello" and the [NAME] entered

We will use  NuSoap library of PHP to write our web service

----------------------------------------------------------------------------------
SOAP Server File Name : greetings-server.php
-----------------------------------------------------------------------------------
First include the [nusoap.php] in your php file

require_once('nusoap.php');

The first thing we need to do is to create the SOAP server.
This is the script that will fetch the data from the database and then deliver it to the Client.

$server = new soap_server;

The Advantage of using NuSOAP library is that this same Server script will also create a WSDL document for us.

The next line is used to tell NuSOAP information for the WSDL document it is going to create for us.
Specifically we specify the name of the server and the namespace, in that order.

$server->configureWSDL('greetserver', 'urn:greetuser');


Now, we register the function we created with the SOAP server.
We pass several different parameters to the register method

$server->register('greetFunction',        // method name
    array('name' => 'xsd:string'),        // input parameters
    array('return' => 'xsd:string'),      // output parameters
    'urn:greetuser',                      // namespace
    'urn:greetuser#hello',                // soapaction
    'rpc',                                // style
    'encoded',                            // use
    'Says hello to the caller'            // documentation
);

Now define your method i.e [greetFunction] as a PHP function

function greetFunction($name)
{
  return 'Hello, ' . $name;
}

Now need  to give a call to the service,before calling the service we need to check
if $HTTP_RAW_POST_DATA is initialize if not just initialize it with a blank string

$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA)? $HTTP_RAW_POST_DATA : '';

//actual call to the service
$server->service($HTTP_RAW_POST_DATA);
----------------------------------------------------------------------------------
SOAP Client    File Name : greetings-client.php
-----------------------------------------------------------------------------------
Now we need a client script which will call our web service

Include the [nusoap.php] in your php file

require_once('nusoap.php');

Then we need to instantiate the soapclient class.
We pass in the URL of the SOAP Server we are dealing with.

$client = new nusoap_client('http://localhost/greetings-server.php', false);

Last make a call to the Web Service.

Here we have to pass the parameters to the Web Service in an array
in which the keys are the names defined for the service i.e
(input parameters when we registered the function with the server)

$result = $client->call('greetFunction', array('name' => 'Tiger'));

Display the Reponse received to our request from the SOAP Server

\r\n

  print_r($result);

\r\n

---------------------------------------------------------------------------------

\r\n


Now when you run the greetings-client.php you will get the following output

Hello, Tiger

 
Categories