Friday, June 5, 2009

Making Webservices with NuSoap

Php can make an excellent webservice. NuSoap is an excellent library to provide standards compilant web services to your audience.

It took me some time to find out recent versions of PHP (5.2+) do not populate
the $HTTP_RAW_POST_DATA properly.So in this case the server part will use
file_get_contents('php://input') to get its data.

Here is a working example.
Server Part:

date_default_timezone_set('Europe/Amsterdam');
require_once('nusoap/lib/nusoap.php');
$server = new soap_server;

function hello($name, $role)
{
return "Hi ".$name." you are a ".$role;
}

$server->register('hello', // method name
array('name' => 'xsd:string', 'role' => 'xsd:string'),
array('return' => 'xsd:string'),
'uri:helloapp',
'uri:helloapp/hello',
'rpc',
'encoded');

$server->service(file_get_contents('php://input'));


Client Part:

date_default_timezone_set('Europe/Amsterdam');
require_once('nusoap/lib/nusoap.php');

$objClient = new soapclient('http://work/soap/server.php');
echo $objClient->call('hello', array('name' => 'John','role' => 'programmer'));

1 comment: