Nusoap Building a PHP-based SOAP Service _php tutorial

Source: Internet
Author: User
Tags http post php soap server soap client

Many organizations have adopted Apach and PHP as their Web application environment. The use of PHP in Web Services mode may seem more difficult. But in fact, with Nusoap, you can easily apply PHP to build soap's client and server side.

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.

List 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);
}
?>

Listing 1 shows 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.

List 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 the NUSOAP registration function and the invocation 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.

List 3: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, get $reversed
";

$n 1 = 5;
$n 2 = 14;
$params 2 = Array (num1=> $n 1, num2=> $n 2);
$added = $client->call (add2numbers, $params 2);
echo "If you add $n 1 and $n 2 get $added
";
?>

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 19

http://www.bkjia.com/PHPjc/508348.html www.bkjia.com true http://www.bkjia.com/PHPjc/508348.html techarticle Many organizations have adopted Apach and PHP as their Web application environment. The use of PHP in Web Services mode may seem more difficult. But in fact, with Nusoap, you can ...

  • 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.