Application of Nusoap to build new Web services based on PHP

Source: Internet
Author: User
Tags http post soap php code php soap server strlen trim
Many organizations have adopted Apach and PHP as their Web application environment. Using PHP in Web Services mode may look more difficult. But in fact, with Nusoap, you can easily use PHP to build soap's client and server side.

An example would explain everything, let's look at one example first

To illustrate how to use Nusoap and PHP to build Web services, we'll give a simple example. This example application consists of a server-side and a client of PHP Web services. He will implement two functions: reverse the order of a string character, and ask for a two-digit number.

PHP SOAP Server
Using PHP and Nusoap to build a SOAP server is easy. Basically, you just write the functions you want to expose to your Web services and then use NUSOAP to register them.

OK, another two steps are required to complete the creation of the PHP soap server. First you have to 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. Look at listing 1 first.

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 to New Soap_fault (' Client ', ', ', ' the ' the ' the ' is invalid ');
}
if (Trim ($num 2)!= intval ($num 2)) {
Return to 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 we would like to expose to Web services through the SOAP protocol: reverse and Add2numbers, which are the core of our Web Services application. The function reverse takes a parameter, reverses the order of the characters in the string, and returns.

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.