Juggling with nuSoap php array’s

Intro:

Started last week with nuSoap php webservices and I must say that the oppertunities of this little library are unlimited.

Me for instance use online mysql-databases for certain projects and would like to build neat little offline applications to manipulate and use those databases, so one word: webservices.

For people with limited resources, nuSoap is a great easy to use library for webservices.

Most usefull info that I found:

Tutorial:

This tutorial gives a small explanation of how to use array’s with nuSoap. The webservice asks for a name and returns an array that says “hello ‘name'”.

First: Install the library’s on the server.

You can download the library’s at SourceForge.

The location on the server is not of relevance (I put mine on http://www.example.com/soap/lib/)

Second: Create the serverobject that makes the service

For example http://www.example.com/soap/hello/server.php

<?php
// load SOAP library
require_once(“../lib/nusoap.php”);

// load library that holds implementations of functions we’re making available to the web service
require_once(“functions.php”);

// set namespace
$ns=”
http://localhost/“;

// create SOAP server object
$server = new soap_server();

// setup WSDL file, a WSDL file can contain multiple services
$server->configureWSDL(‘HelloWorld’,$ns);
$server->wsdl->schemaTargetNamespace=$ns;

//Add complex type
$server->wsdl->addComplexType(
  ‘myarray’,                                         //Complextype Name
  ‘complexType’,                              //Complextype type
  ‘array’,                                              //Php type
  ”,                                                         //compositor (all|sequence|choice)
  ‘SOAP-ENC:Array’,                       //restrictionBase namespace:name (
http://schemas.xmlsoap.org/soap/encoding/:Array)
  array(),                                                 //elements = array ( name = array(name=>”,type=>”) )
  array(                                               // attributes
    array(‘ref’ => ‘SOAP-ENC:arrayType’,
         ‘wsdl:arrayType’ => ‘xsd:string[]’)
  ),
  ‘xsd:string’         //arrayType
);

// register a web service method
$server->register(‘ws_hello’,
 array(‘input’ => ‘xsd:string’),                       // input parameters
 array(‘return’ => ‘tns:myarray’),              // output parameter
 ‘HelloWorld’,                                                                         // namespace
    “HelloWorld#ws_hello”,                                            // soapaction
    ‘rpc’,                                                                     // style
    ‘encoded’,                                                          // use
    ‘Say hello’                                                          // documentation
 );

//webservice method
function ws_hello($name, $bla){

return new soapval(‘return’,’tns:myarray’,sayhello($name)); //Using the functions.php file for clarity reasons
}

// service the methods
$server->service($HTTP_RAW_POST_DATA);
?>

As you may notice, the server creates a new service: ‘HelloWorld’
The service has 1 complexType: MyArray and 1 operation: ws_hello.
The operation asks for a string and gives the complex type back.

You can examine the service at http://www.example.com/soap/hello/server.php
And the wsdl at http://www.example.com/soap/hello/server.php?wsdl

I also seperated my function logic from my service by using a second file functions.

Third: Create the file functions

For example http://www.example.com/soap/hello/functions.php

<?php
// implementation of  hello function
function sayhello($name) {
 return array(“Hello”, ” “, $name);
}
?>

The function returns an array with 3 strings.

Last: Client implementation

For example http:://www.example.com/soap/hello/Client.php

<?php
//Call library’s
require_once(‘../lib/nusoap.php’);

//Set wsdl
$wsdl=”
http://www.example.com/soap/hello/server.php?wsdl“;

//Set client
$client=new soapclient($wsdl, ‘wsdl’);

//create input array
$input=array(‘input’=>’Niels’);

//call webservice
$result = $client->call(‘ws_hello’,$input);

//Walk through array
foreach ($result as $value) {
   echo $value;
}
?>

The client creates a new soapclient and calls the method through the new soapclient. the call gives back the new made array and prints it on the screen.

As you can see, it’s very easy to create a webservice that uses an array, maybe later i’m gonna make an example using structs in my array’s.

Because everything works with wsdl you can easily create a java or .net client.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s