Every application developer needs the ability to develop the services for wide range of platforms. We have SOAP (Simple Object Access Protocol) to create such a service and interact with the software with ease. So in this tutorial I will explain you how you can create a web service using PHP library nusoap.
nusoap library : It’s very easy to create SOAP service. Download the nusoap library from http://sourceforge.net/projects/nusoap/. You need a server for respond to the client after necessary function.
Create a file server.php and copy following code into the file. Now we have two function registered HelloWorld and AddValues.
ini_set('error_reporting', E_STRICT); require_once("nusoap/lib/nusoap.php"); $namespace = "www.demos.tricksofit.com"; // create a new soap server $server = new soap_server(); $server->soap_defencoding = 'utf-8'; $server->decode_utf8 = false; // configure our WSDL $server->configureWSDL("TestWsdl"); // set our namespace $server->wsdl->schemaTargetNamespace = $namespace; //Register a method that has parameters and return types $server->register( // method name: 'HelloWorld', // parameter list: array('name'=>'xsd:string'), // return value(s): array('return'=>'xsd:string'), // namespace: $namespace, // soapaction: (use default) false, // style: rpc or document 'rpc', // use: encoded or literal 'encoded', // description: documentation for the method 'Simple Hello World Method' ); //Register a method that has parameters and return types $server->register( // method name: 'AddValues', // parameter list: array('a'=>'xsd:int','b'=>'xsd:int'), // return value(s): array('return'=>'xsd:int'), // namespace: $namespace, // soapaction: (use default) false, // style: rpc or document 'rpc', // use: encoded or literal 'encoded', // description: documentation for the method 'Simple add method' ); //first function implementation function HelloWorld($name) { return 'Howdy, '.$name.'!'; } function AddValues($a, $b) { return $a+$b; } $POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : ''; // pass our posted data (or nothing) to the soap service $server->service($POST_DATA); exit();
You can see XML structure of your soap server using server.php?wsdl
Now you need to create client service to call HelloWorld and AddValues method using soap. So create a file client.php and put following code and save.
<!doctype html> <html> <head> <title>Title</title> <meta charset="utf-8"/> </head> <body> <?php require_once("nusoap/lib/nusoap.php"); $client = new nusoap_client('path to /server.php?wsdl'); $client->soap_defencoding = 'UTF-8'; $client->decode_utf8 = false; $err = $client->getError(); if ($err) { echo ' <h2>Constructor error</h2> <pre>' . $err . '</pre> '; die(); } $parameters = array('name' => "dear"); $result = $client->call('HelloWorld', $parameters); //$parameters = array('a' => "2", 'b' => "3"); //$result = $client->call('AddValues', $parameters); if ($client->fault) { echo ' <h2>Fault</h2> <pre>'; print_r($result); echo '</pre> '; die(); } else { echo $result; } ?> </body> </html>
Now when you run the client with following code:
$parameters = array('name' => "dear"); $result = $client->call('HelloWorld', $parameters);
You will get Howdy, dear! as result.
When you use following code:
$parameters = array('a' => "2",'b' => "3"); $result = $client->call('AddValues', $parameters);
you will get 5 as a result.
Hope this tutorial will help you to create a web service using nusoap library.