How to use the PHP SOAP module: non-wsdl mode

Source: Internet
Author: User
Tags php server

The PHP SOAP extension can help us implement the Web service very easily, there are two main modes of operation in the SOAP extension of PHP: The WSDL mode and the NON-WSDL mode, the former uses the WSDL file name as the parameter, and extracts the information used by the service from the WSDL. The latter is to pass the information that is to be used by the way that passes the parameter. Lee bad today mainly for everyone to share the simple use of non-wsdl mode.
First, let's look at the three main objects of the SOAP extension:

1.SoapServer

SoapServer defines functions that can be called and returns response data when creating PHP server-side pages. The syntax format for creating a non-wsdl-mode SoapServer object is as follows:

$soap New SoapServer (null,$arr);

$arr is the property information for SoapServer, which is an array.
The AddFunction method of the SoapServer object is used to declare which function can be called by the client, using the following method:

$soap->addfunction ($functionName);

$functionName is the name of the method that the client is allowed to invoke. If all the methods are allowed to be called, use the following method:

$soap->addfunction (Soap_functions_all);

The handle method of the SoapServer object is used to process the user input and invoke the corresponding function, and finally returns the result to the client for processing. Here's how to use it:

$soap->handle ([$soapRequest]);

$soapRequest is an optional parameter that represents the user's request information. If you do not specify $soaprequest, it means that the server will receive all requests from the user.

2.SoapClient

SoapClient is used to invoke the SoapServer page on the remote server and implements a call to the corresponding function, creating a SoapClient object as follows:

$client New SoapClient (null,$arr);

Where the parameter $arr is the same as SoapServer. After wearing a SoapClient object, call the server-side method as follows:

$client->functionname ($p);

FunctionName () is the name of the function to be called on the server side, $p as a parameter.

3.SoapFault

SoapFault is used to generate errors that may occur during SOAP access. To create a SoapFault object, the method is:

$fault New SoapFault ($code,$msg);

The parameter $code is a user-defined error code $msg a user-defined error message. The SoapFault object is generated automatically when an error occurs on the server-side page, or when the user creates the SoapFault object themselves. For errors that occur with SOAP access, the client can obtain the appropriate error message by capturing the Soapfalut object. After the client captures the SoapFault object, you can obtain the error code and error message by using the following code:

$fault->code;  // Error code $fault->msg;   // error Message

The above is the PHP Soap module main Three object introduction, below we through a simple example, to help you understand the use of the SOAP module.

4.PHP Soap Instance (non-wsdl mode)

Server-side code server.php:

$arr=Array(            ' Location ' = ' http://192.168.187.132/server.php ', ' uri ' = ' http://192.168.187.132/'        ); $soap=NewSoapServer (NULL,$arr ); $soap->addfunction (Soap_functions_all);//allow clients to invoke all methods        $soap-handle (); functionA () {return' Lee Bad blog '; }          functionb$b){            return $b; }   

Client code:

$arr Array (            ' location ' = ' http://192.168.187.132/server.php ',            ' uri '      = ' http://192.168.187.132/ '    );          $soap New NULL $arr  );                  Echo $soap->a (), ' <br/> ';         Echo $soap->b (' B ');     

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.