Home >Backend Development >PHP Tutorial >Applying NuSoap to build a new PHP-based Web service_PHP tutorial

Applying NuSoap to build a new PHP-based Web service_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:11:40807browse

Many organizations have adopted Apach and PHP as their Web application environment. Adopting PHP in Web services mode may seem difficult. But in fact, with NuSoap, you can easily use PHP to build SOAP client and server.

An example can explain everything, let's take a look at an example first

To illustrate how to use NuSoap and PHP to build Web services, we will give a simple example. This example application consists of a PHP Web services server and client. It will implement two functions: reversing the order of characters in a string and finding the sum of two numbers.

PHP SOAP Server
It is very easy to set up a SOAP server with PHP and NuSoap. Basically, you just write the functions you want to expose to your Web services and register them with NuSoap.

OK, two more steps are needed to complete the establishment 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. Sounds simple? Let’s take a look at Listing 1 first.

Listing 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++) {
                                                                                           
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 gives the source file of soapfunc.php. This file contains two functions that we want to expose to Web services through the SOAP protocol: reverse and add2numbers, which are the core of our Web services application. The reverse function takes one argument, reverses the order of the characters in the string, and returns it.



http://www.bkjia.com/PHPjc/629558.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629558.htmlTechArticleMany organizations have adopted Apach and PHP as their Web application environment. Adopting PHP in Web services mode may seem difficult. But in fact, with NuSoap, you can easily...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn