Use NuSoap to build a new PHP-based Web Service

Source: Internet
Author: User
Tags php soap server

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.

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.