Use nusoap to build a new PHP-based Web Service

Source: Internet
Author: User
Tags soap php soap server soap client
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. This example is used.ProgramIt consists of a PHP Web services server and a client. 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 needCodeCreate an instance of the nusoap object, and then use the http post method to transmit the original data to nusoap for processing. It sounds simple. First, let's look at listing 1.

List 1: soapfunc. php
require_once ('nusoap. PHP ');
function reverse ($ Str) {
$ retval = "";
If (strlen ($ Str) <1) {
return New soap_fault ('client', '', 'invalid string');
}< br> for ($ I = 1; $ I <= strlen ($ Str); $ I ++) {
$ retval. = $ STR [(strlen ($ Str)-$ I)];
}< br> return $ retval;
}

function add2numbers ($ num1, $ num2) {
If (TRIM ($ num1 )! = Intval ($ num1) {
return New soap_fault ('client', '', 'the first number is invalid ');
}< br> If (TRIM ($ num2 )! = Intval ($ num2) {
return New soap_fault ('client', '', 'The second number is invalid ');
}< br> return ($ num1 + $ num2);
}< BR >?>
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' <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 ";
?>

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

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.