Many organizations have adopted Apach and PHP as their Web application environments. Using PHP in the Web services mode may seem difficult. But in fact, with NuSoap, you can easily use PHP to build SOAP clients and servers.
One example can illustrate everything. Let's take a look at one example.
To illustrate how to use NuSoap and PHP to build Web services, we will give a simple example. The application in this example is composed of a server and a client of PHP Web services. He will implement two functions: reversing the character order of a string and finding the sum of two numbers.
Php soap server
It is very easy to create a SOAP server using PHP and NuSoap. Basically, you only need to write the functions that you want to expose to your Web services, and then register them with NuSoap.
OK. In addition, two steps are required to establish the PHP SOAP server. First, you need to create an instance of the NuSoap object in your PHP code, and then use the http post method to pass the original data to NuSoap for processing. It sounds simple. First, let's look at listing 1.
Listing 1: soapfunc. php
<?
Require_once ('nusoap. php ');
Function reverse ($ str ){
$ Retval = "";
If (strlen ($ str) <1 ){
Return new soap_fault ('client', '', 'invalidstring ');
}
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 shows the source file soapfunc. php. This file contains two functions that we want to expose to Web services through the SOAP protocol: reverse and add2numbers. They are the core of our Web services application. The reverse function contains a parameter that reverses the character order in the string and returns the result.