Web services server on PHP

The easiest and the most portable solution for a web service server is a NuSOAP library.
It’s VERY useful when one has nothing but an FTP access to a remote hosting website.
All you need is just copy-paste the library and it works.

So… let’s proceed to create the simplest web server script.
It will just accept String value and return String value (that is modified based on the accepted value).

1. Create a folder named “soap_test” (or whatever …) within your remote website.
Copy-paste “lib” into that folder and rename it to “soap_lib” (or whatever…).
“Lib” is taken from here:
http://sourceforge.net/project/showfiles.php?group_id=57663
(don’t worry, there is just one lib, so you shouldn’t miss it)

2. Within the “soap_test” folder create the file named server.php (again, or whatever you want) and start coding:

3. Include the library and a namespace:
require_once(“soap_lib/nusoap.php”);
$namespace=”http://www.examplesite.com/soap_test“;

4. Some settings:
$server = new soap_server();
$server->configureWSDL(“soap_test”, $namespace);
$server->wsdl->schemaTargetNamespace = $namespace;

5. Declare the function and describe what (arguments) it will accept and return:
$server->register(‘ReturnConfirmNumber’,
array(‘amount’ => ‘xsd:string’),
array(‘return’ => ‘xsd:string’),
$namespace);

6. Write the function itself:
function ReturnConfirmNumber($amount){
$result=$amount . “_this_was_appended_by_server!”;
return new soapval(‘return’,’xsd:string’,$result);
}

7. Finally make a lazy server do what it was born for:
$server->service($HTTP_RAW_POST_DATA);
?>

8. Now time to test it, go to:
http://www.examplesite.com/soap_test/server.php?wsdl

Here you should see some XML file guts… if you see them! You are done!
Congratulations! … let’s go further >>>

To accept more input parameters add value pairs within the first array and separate them by comma.
Ex:
array(‘name’ => ‘xsd:string’, ‘company’ => ‘xsd:string’, ‘initialname’ => ‘xsd:string’ ),

Sources used:
http://webservices.xml.com/lpt/a/1388

To consume web services in ASP.NET refer to “web programming” -> ASP (.NET) -> Web services client on VB.NET

Leave a comment