PHP + NuSoap: A simple webservice
December 20th, 2008
Have you ever needed to build a webservice?
After some googling i found a library called nusoap, full compatible with PHP4 and PHP5, very simple and intuitive.
Let me illustrate a very simple example of use this library for building webservices in PHP.
MyWebService.php
-
-
$namespace = "http://foo.bar.com/";
-
require("lib/nusoap.php");
-
$server = new soap_server();
-
$server->debug_flag = false;
-
$server->configureWSDL("MyWebService", $namespace);
-
$server->wsdl->schemaTargetNamespace = $namespace;
-
-
// method name to expose
-
$server->register('myMethod',
-
// parameters
-
array('param1' => 'xsd:string', 'param2' => 'xsd:string'),
-
// return value
-
array('return' => 'xsd:string'),
-
// namespace
-
$namespace,
-
// soapaction
-
$namespace . '#myMethod',
-
// style
-
'rpc',
-
// use
-
'encoded',
-
// method description
-
'Concatenate two strings'
-
);
-
-
function myMethod($param1, $param2){
-
return $param1.$param2;
-
}
-
-
$HTTP_RAW_POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA'])?
-
$GLOBALS['HTTP_RAW_POST_DATA'] : '';
-
$server->service($HTTP_RAW_POST_DATA);
-
exit();
With this few lines of code, we made a webservice, callable at this url:
http://foo.bar.com/MyWebService.php
This exposes a method MyMethod that takes two string parameters and concatenate them.






Last year for a project involving travelport xml api integration, we badly needed the soap requests to be gzipped, since the technical support people suggested.
http://www.php-trivandrum.org/tips/gzip-nusoap-requests.html