Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Creating Webservice Server and Client Using NuSOAP

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Creating webservice server and client using NuSOAP

Posted by admin on Jun 28, 2012 in php | 2 comments Brief description of NuSOAP: NuSOAP is a rewrite of SOAPx4, provided by NuSphere and Dietrich Ayala. It is a set of PHP classes no PHP extensions required that allow developers to create and consume web services based on SOAP 1.1, WSDL 1.1 and HTTP 1.0/1.1. Why using NuSOAP? PHP5 already has soapServer and soapClient implementation

its easy to implement and fast, no need to create WSDL document by yourself (this is my main reason why I choose NuSOAP) compatibility, no special PHP extension need (I didnt test it if still works on PHP4, but it does work flawlessly on PHP 5.0)

First, download NuSOAP from here: http://sourceforge.net/projects/nusoap/ The Server We will create a SOAP server with two entry points (=function). One function take one parameter and output a string while the other one take two parameters and output a complex result (an array/struct). The codes with commentary:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 require_once('lib/nusoap.php'); $server = new nusoap_server; $server->configureWSDL('server', 'urn:server'); $server->wsdl->schemaTargetNamespace = 'urn:server'; //SOAP complex type return type (an array/struct) $server->wsdl->addComplexType( 'Person', 'complexType', 'struct', 'all', '', array( 'id_user' => array('name' => 'id_user', 'type' => 'xsd:int'), 'fullname' => array('name' => 'fullname', 'type' => 'xsd:string'), 'email' => array('name' => 'email', 'type' => 'xsd:string'), 'level' => array('name' => 'level', 'type' => 'xsd:int') ) ); //first simple function $server->register('hello', array('username' => 'xsd:string'), //parameter array('return' => 'xsd:string'), //output

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

'urn:server', //namespace 'urn:server#helloServer', //soapaction 'rpc', // style 'encoded', // use 'Just say hello'); //description //this is the second webservice entry point/function $server->register('login', array('username' => 'xsd:string', 'password'=>'xsd:string'), //parameters array('return' => 'tns:Person'), //output 'urn:server', //namespace 'urn:server#loginServer', //soapaction 'rpc', // style 'encoded', // use 'Check user login'); //description //first function implementation function hello($username) { return 'Howdy, '.$username.'!'; } //second function implementation function login($username, $password) { //should do some database query here // .... ..... ..... ..... //just some dummy result return array( 'id_user'=>1, 'fullname'=>'John Reese', 'email'=>john@reese.com, 'level'=>99 ); } $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ''; $server->service($HTTP_RAW_POST_DATA);

Suppose that you save the codes in ws.php, you can see your neat webservice description by accessing the URL: http://your_server_address/ws.php Try click on one of your webservice entry point! NuSOAP will show you a description about it, see screenshot:

example of entry point description generated by NuSOAP for WSDL XML document, you can see it at: http://your_server_address/ws.php?wsdl The client The code is quite simple, all the code need is your webservice WSDL URL.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 require_once('lib/nusoap.php'); //This is your webservice server WSDL URL address $wsdl = "http://localhost/nusoap_test/ws.php?wsdl"; //create client object $client = new nusoap_client($wsdl, 'wsdl'); $err = $client->getError(); if ($err) { // Display the error echo '<h2>Constructor error</h2>' . $err; // At this point, you know the call that follows will fail exit(); } //calling our first simple entry point $result1=$client->call('hello', array('username'=>'achmad')); print_r($result1); //call second function which return complex type $result2 = $client->call('login', array('username'=>'john', 'password'=>'doe') ); //$result2 would be an array/struct print_r($result2);

Thats it! Creating webservice server and client using NuSOAP is fast and simple.

You might also like