Use NuSoap to build a new PHP-based Web service

Source: Internet
Author: User
Tags php soap server soap client
Many organizations have adopted Apach and PHP as their Web application environments. Using PHP in the Webservices mode may seem difficult. But in fact, with NuSoap, you can easily use PHP to build SOAP clients and servers. Let's look at an example to illustrate how to use NuSoap and PHP to build w soa.

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.

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 how to register a function and call a SOAP handler using NuSoap. As you can see, registering your two functions (in soapfunc. php) and passing POST data to the soap_server object are just as simple as creating a new soap_server object instance. The soap_server object will check the POST data, determine which function will be called, and then pass parameters to this PHP function. The data returned from the PHP function is repackaged as a SOAP response and sent to the SOAP client of the request service.

Php soap client
Now we have created a SOAP server using NuSoap and PHP, and we need to test it. Like creating a SOAP server program, we can use NuSoap in PHP to create a SOAP client program. Listing 3 shows the source code of the php soap client program.

Now we have created a SOAP server using NuSoap and PHP, and we need to test it. Like creating a SOAP server program, we can use NuSoap in PHP to create a SOAP client program. Listing 3 shows the source code 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'
\ 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
\ N ";
?>



To use the PHP client on a SOAP server, you have to do three things. First, you need to create a soapclient object. The soapclient object is responsible for parameter grouping and SOAP protocol processing. The soapclient must be a URL parameter. This URL can point to an HTTP terminal of the actual SOAP server or a WSDL description. In our example, it is a URL pointing to our php soap server.

When calling a function that requires parameters, you must first create a parameter array, which includes a set of key-value pairs. Key is the name of the parameter, and value is the value of the parameter.

When you need to call a function, you can use the soapclient object to call the function and input two parameters. The first parameter is the name of the function you want to call. The second parameter is an array containing the parameters of the SOAP function. The main function will return the value of the SOAP function you call.

To run this example, simply enter the URL of soapclient. php in the Web browser. You will output something similar to the following:

If you reverse 'This string will be reversed ', you get 'srever eb lliw gnirts sihT' If you add 5 and 14 you get 19
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.