An example would explain everything, let's look at an example first
To illustrate how to use Nusoap and PHP to build Web services, we'll give you a simple example. This example application consists of a PHP Web services server side and a client. He will implement two functions: reverse the order of a string character and ask for two numbers of the number.
PHP SOAP Server
It is very easy to build a SOAP server with PHP and Nusoap. Basically, you just write out the functions you want to expose to your Web services, and then register them with Nusoap.
OK, another two steps will be required to complete the creation of the PHP soap server. First you will also 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. It sounds simple. Let's take a look at 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 ($num 1, $num 2) { if (Trim ($num 1) != intval ( $num 1)) { return new soap_fault (client, , the first number is invalid); } if (Trim ($num 2) != intval ($num 2)) { return new soap_fault (client, , the second number iS invalid); } return ($num 1 + $num 2); }?>
The source file for the soapfunc.php. This file contains two functions that we want to expose to Web services through the SOAP protocol: reverse and Add2numbers, which are at the heart of our Web Services application. The reverse function takes a parameter, reverses the order of the characters in the string, and then returns.
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);? >
Nusoap registers a function and invokes the use of a soap handler. You see, registering your two functions (in soapfunc.php), passing the post data to the Soap_server object is as simple as creating a new Soap_server object instance. The Soap_server object will examine the post data and determine which function will be called, and pass the arguments to the PHP function. The data returned from the PHP function is repackaged as a SOAP response and routed to the SOAP client requesting the service.
PHP SOAP Client
Now that we have created a SOAP server with Nusoap and PHP, we need to test it. Like we build a SOAP server program, we can use Nusoap in PHP to build a SOAP client program. Listing 3 shows the source program for the PHP SOAP client program.
Now that we have created a SOAP server with Nusoap and PHP, we need to test it. Like we build a SOAP server program, we can use Nusoap in PHP to build a SOAP client program. Listing 3 shows the source program for the PHP SOAP client program.
soapclient.php
<?include (nusoap.php); $client = new SoapClient (http://localhost/soapserver.php); $str = "This string would be reversed "; $params 1 = array (str=> $str); $reversed = $client->call (reverse, $params 1); echo" If you reverse $str, you get $rever Sed<br>\n "; $n 1 = 5; $n 2 = +; $params 2 = Array (num1=> $n 1, num2=> $n 2); $added = $client->call (add2numbers, $ PARAMS2); echo "If you add $n 1 and $n 2 get $added <br>\n";? >
To be able to use PHP client programs on a SOAP server, you have to do three things. First, you want to create a SoapClient object. The SoapClient object is responsible for handling the grouping of parameters and the SOAP protocol. SoapClient requires a parameter that must be a URL. This URL can point to an HTTP terminal of an actual SOAP server or a WSDL description. In our example, it is a URL that points to our PHP soap server.
When calling a function that requires a parameter, you need to first create an array of arguments, including a set of key-value (key-value) pairs. The key (key) is the name of the parameter, and the value is the value of the parameter.
When you need to invoke a function, you use the SoapClient object to invoke 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 that will return the value of the SOAP function that you called.
To run this example, simply enter the URL of the soapclient.php in your Web browser. You will have output similar to the following:
If you reverse this string would be reversed, you get desrever eb lliw gnirts siht If you add 5 and + you get 1Array
Application of Nusoap to build new PHP-based Web Services