Many organizations have adopted Apach and PHP as their Web application environment. Adopting PHP in Web services mode may seem difficult. But in fact, with NuSoap, you can easily use PHP to build SOAP client and server.
To illustrate how to use NuSoap and PHP to build Web services, we will give a simple example. This example application consists of a PHP Web services server and client. It will implement two functions: reversing the order of characters in a string and finding the sum of two numbers.
PHP SOAP Server Setting up a SOAP server is very easy with PHP and NuSoap. Basically, you just write the functions you want to expose to your Web services and register them with NuSoap.
OK, there are two more steps required to complete the establishment of the PHP SOAP server. First you have to create an instance of the NuSoap object in your PHP code, and then use the HTTP POST method to pass the raw data to NuSoap for processing. Sounds simple? Let’s take a look at Listing 1 first.
Listing 1: soapfunc.php
<?
require_once('nusoap.php');
function reverse($str){
$retval = "";
if(strlen($str) < 1) {
return new soap_fault('Client','','Invalid string');
}
for ($i = 1; $i <= strlen($str); $i++) {
$retval .= $str[(strlen($str) - $i)];
}
return $retval;
}
function add2numbers($num1, $num2) {
if (trim($num1) != intval($num1)) {
return new soap_fault('Client', '', 'The first number is invalid');
}
if (trim($num2) != intval($num2)) {
return new soap_fault('Client', '', 'The second number is invalid');
}
return ($num1 + $num2);
}
?>
Listing 1 gives the source file of soapfunc.php. This file contains two functions that we want to expose to Web services through the SOAP protocol: reverse and add2numbers, which are the core of our Web services application. The reverse function takes one argument, reverses the order of the characters in the string, and returns it.
Listing 2: soapserver.php
<?
require_once('nusoap.php');
include('soapfunc.php');
$soap = new soap_server;
$soap->register('reverse');
$soap->register('add2numbers');
$soap->service($HTTP_RAW_POST_DATA);
?>
Listing 2 illustrates the use of NuSoap registration functions and calling SOAP handlers. As you see, registering your two functions (in soapfunc.php) and passing the POST data to the soap_server object is as easy as creating a new instance of the soap_server object. The soap_server object will examine the POST data and determine which function to call, then pass the parameters to this PHP function. The data returned from the PHP function is repackaged into a SOAP response and delivered to the SOAP client requesting the service.
PHP SOAP Client Now that we have created a SOAP server using NuSoap and PHP, we need to test it. Just like we established a SOAP server program, we can use NuSoap to create a SOAP client program in PHP. Listing 3 gives the source program of the PHP SOAP client program.
Now that we have created a SOAP server using NuSoap and PHP, we need to test it. Just like we established a SOAP server program, we can use NuSoap to create a SOAP client program in PHP. Listing 3 gives the source program of the PHP SOAP client program.
Listing 3: soapclient.php
<?
include('nusoap.php');
$client = new soapclient('http://localhost/soapserver.php');
$str = "This string will be reversed";
$params1 = array('str'=>$str);
$reversed = $client->call('reverse',$params1);
echo "If you reverse '$str', you get '$reversed'<br>n";
$n1 = 5;
$n2 = 14;
$params2 = array('num1'=>$n1, 'num2'=>$n2);
$added = $client->call('add2numbers', $params2);
echo "If you add $n1 and $n2 you get $added<br>n";
?>
In order to use a PHP client program on a SOAP server, you have to do three things. First, you need to create a soapclient object. The soapclient object is responsible for handling the marshalling of parameters and the SOAP protocol. soapclient requires a parameter which must be a URL. This URL can point to an HTTP endpoint or a WSDL description of the actual SOAP server. In our case, it's a URL pointing to our PHP SOAP server.
When calling a function that requires parameters, you need to first create a parameter array, which contains a set of key-value pairs. The key is the name of the parameter, and the value is the value of the parameter.
When you need to call a function, you use the soapclient object to call it and pass in two parameters. The first parameter is the name of the function you want to call, and the second parameter is an array containing the parameters of the SOAP function. The calling function will return the value of the SOAP function you called.
To run this example, simply enter the URL to soapclient.php in your web browser. You will have output similar to:
If you reverse 'This string will be reversed', you get 'desrever eb lliw gnirts sihT' If you add 5 and 14 you get 19