Use of PHP and WebServices-nusoap

Source: Internet
Author: User
You can search for nusoap on the Internet.

As follows:

Nusoap is a Web Service programming tool in the PHP environment. It is used to create or call Web Services. It is an open-source software. The current version is 0.7.2. It supports soap1.1 and wsdl1.1 and can interoperate with other systems that support soap1.1 and wsdl1.1. Nusoap is completely compiled by the PHP language and consists of a series of PHP classes without the support of extension libraries. This feature allows nusoap to be used in all PHP environments and is not affected by server security settings.

 

1. obtain and install nusoap

The nusoap project is built on SourceForge and the network address is: http://sourceforge.net/projects/nusoap/ or http://dietrich.ganx4.com/nusoap, where the latest version of nusoap can be downloaded.

The installation of nusoap is relatively simple. Copying the downloaded nusoap file to the server can be stored in an independent directory orProgramCodePut it in the same directory, as long as your PHP code can access these files.

The test environment in this article is based on php4.3.2 and nusoap 0.7.2. nusoap is installed in the "/nusoap" web directory and has two subdirectories: Lib and samples. The lib directory stores all nusoapSource codeFile, which is provided by the nusoap development team under the samples directory. The test file is stored in the "/nusoap" web directory.

 

2. Use of nusoap

Nusoap is composed of PHP classes, the most common of which are soap_server classes and soalclient classes. The soap_server class is used to create Web Services and soapclient class is used to access web services.

2.1 A simple example: Hello World

In this example, a simple Web Service is created using nusoap, and a client program is created using nusoap to call this service. The only function of this service is to return a string "Hello World" to the client ". First, create the Web service program code file "/nusoap/nusoap_server1.php ":

// Include the source file of nusoap into the current code file

<? PHP

Require_once ("lib/nusoap. php ");

// Define the Service Program

Function Hello () {return 'Hello world! ';}

// Initialize the service object, which is an instance of the soap_server class

$ Soap = new soap_server;

 

// Call the Register Method of the service object to register the program to be accessed by the client.

// Only registered programs can be accessed by remote clients.

$ Soap-> Register ('hello ');

 

// The last step is to pass the data submitted by the client through post to the service method of the service object.

// The service method processes the input data, calls the corresponding function or method, generates correct feedback, and returns it to the client.

$ Soap-> service ($ http_raw_post_data );

?>

 

Now, the Web service program code file has been created. Next, create a client program code file "/nusoap/nusoap_client1.php" and call the Web Service:

// Include the source file of nusoap into the current code file

<? Phprequire_once ("lib/nusoap. php ");

// Initialize the client object, which is an instance of soapclient class,

// Pass the URL of the service program to the constructor of the soapclient class.

$ Client = new soapclient ('HTTP: // 127.0.0.1/nusoap/nusoap_server1.php ');

 

// Use the call method of the client object to call the Web Service Program $ STR = $ client-> call ('hello ');

 

// The geterror () method of the client object can be used to check whether an error occurs during the call process.

// If there is no error, the geterror () method returns false; if there is an error, the geterror () method returns an error message.

If (! $ Err = $ client-> geterror ()){

Echo "program return :",

Htmlentities ($ STR, ent_quotes );

} Else {

Echo "error :",

Htmlentities ($ err, ent_quotes );

}

?>

 

Now, the client program has been established. Open the browser, access the client program, and check the result. In this example, the browser will display a string: "The program returns: Hello world! "

2.2 Methods for passing parameters and returning error messages

The example shows how to pass parameters and return error messages. In this example, two strings are connected. The parameters are two strings, and the returned values are strings connected by two parameters. First, create the service program code file "/nusoap/nusoap_server2.php". The complete code is as follows:

 

<? Phprequire_once ("lib/nusoap. php ");

Function concatenate ($ str1, $ str2 ){

If (is_string ($ str1) & is_string ($ str2 ))

Return $ str1. $ str2;

Else

Return new soap_fault ('client', '', 'parameter of the concatenate function should be two string ');

}

$ Soap = new soap_server; $ soap-> Register ('concatenate ');

$ Soap-> service ($ http_raw_post_data );

?>

 

Compared with the code of the web service program in section 2.1, the code structure here is roughly the same. Note the following:

    • Different service programs have two parameters. The process of registering a service program using nusoap is the same. The Register Method of the service object is called.
    • Here we use a new nusoap class soap_fault. When either of the two parameters is not a string, the program returns the error message to the client through this class. The constructor of this class has four parameters:

Fault

Code

Required parameter. The recommended value is "client" or "server", indicating whether the error is a client error or a server error.

Faultactor

Reserved item, not used yet

Faultstring

Error description

Faultdetail

Optional, XML format data, detailed error information

The complete content of the client code file "/nusoap/nusoap_client2.php" is as follows:

 

<? Phprequire_once ("lib/nusoap. php ");

$ Client = new soapclient ('HTTP: // 127.0.0.1/nusoap/nusoap_server2.php ');

$ Parameters = array ('string 1', 'string 2 ');

$ STR = $ client-> call ('concatenate', $ parameters );

If (! $ Err = $ client-> geterror ()){

Echo "program return:", $ STR;

} Else {

Echo "error:", $ err ;}

?>

 

When a nusoap client calls a web service with parameters, it uses arrays to pass parameters. $ Parameters is an array with values of each parameter in sequence. When the client calls a remote service program, it uses the call method with two parameters. The first parameter is the name of the Service Program, and the second parameter is the parameter array of the service program, here is $ parameters. Access the client program in the browser, and the browser will display a string: "program return: String 1 string 2"

Next, try to pass in the error parameter to the Web Service Program, modify the above client program, and change the statement that generates the parameter array to: $ parameters = array ("string", 12 ), then access the client program through the browser, and the browser will display the string: "error: the client: concatenate function parameter should be two strings ". The Web Service Program judges that the input parameter is not a string and returns an error message to the client through soap_fault.

2.3 debugging method

There are three common debugging methods in nusoap:

2.3.1 request and response member variables of the soapclient class

The most direct debugging method is to check the request information sent by the client and the response information returned by the server during web service access. The request and response member variables of the soapclient class contain this information. The content of these two variables is displayed in the program, which can help analyze the program running. See the following code:

 

<? Phprequire_once ("lib/nusoap. php ");

$ Client = new soapclient ('HTTP: // 127.0.0.1/nusoap/nusoap_server2.php ');

$ Parameters = array ('string 1', 'string 2 ');

$ STR = $ client-> call ('concatenate', $ parameters );

If (! $ Err = $ client-> geterror ()){

Echo "program return:", $ STR;

} Else {

Echo "error:", $ err;

}

// The content of the request and response variables is shown below

Echo '<p/>'; echo 'request :';

Echo '<PRE>', htmlspecialchars ($ client-> request, ent_quotes), '</PRE> ';

Echo 'response :';

Echo '<PRE>', htmlspecialchars ($ client-> response, ent_quotes), '</PRE>';?>

 

2.3.2 debug_str member variable of the soapclient class

The debug_str member variable of the soapclient class provides more detailed debugging information. You can view the content of this variable to better help program debugging.

2.3.3 debugging methods provided by web service programs

In the Web service program code, before creating an instance of the soap_server class, define the variable $ DEBUG = 1. The debugging information is used as a comment and is placed at the end of the SOAP message to return to the client. The client can view the debugging information by viewing the response information of the web service.

 

<? Phprequire_once ("lib/nusoap. php ");

Function concatenate ($ str1, $ str2 ){

If (is_string ($ str1) & is_string ($ str2 ))

Return $ str1. $ str2;

Else

Return new soap_fault ('client', '', 'parameter of the concatenate function should be two string ');

}

$ DEBUG = 1; // define debugging

$ Soap = new soap_server;

$ Soap-> Register ('concatenate ');

$ Soap-> service ($ http_raw_post_data );

?>

 

2.4 support for WSDL

Nusoap supports WSDL through the "WSDL" class. For nusoap users, you do not need to worry about how the internal WSDL class works. You can use the soap_server class and soapclient class correctly to support the WSDL.

2.4.1 create a web service that supports WSDL

To support WSDL by web service programs, you must use the configurewsdl method of soap_server and provide more detailed parameters when you call the Register Method of soap_server to register a web service program. See the following code. The code file name is "/nusoap/nusoap_server3.php ".

 

<? Phprequire_once ("lib/nusoap. php ");

Function concatenate ($ str1, $ str2 ){

If (is_string ($ str1) & is_string ($ str2 ))

Return $ str1. $ str2;

Else

Return new soap_fault ('client', '', 'parameter of the concatenate function should be two string ');

}

$ Soap = new soap_server;

$ Soap-> configurewsdl ('concatenate ');

// Initialize the support for WSDL

// Register the service

$ Soap-> Register ('concatenate', array ("str1" => "XSD: string", "str2" => "XSD: string "), // input parameter definition

Array ("return" => "XSD: string") // return Parameter Definition

);

$ Http_raw_post_data = isset ($ http_raw_post_data )? $ Http_raw_post_data :'';

$ Soap-> service ($ http_raw_post_data);?>

Open your browser and access the file you just created, http: // 127.0.0.1/nusoap/nusoap_server3.php. The result is as follows:

Concatenate

ViewWSDLFor the service. Click on an operation name to view it & apos; s details.

    • Concatenate

Click the function name concatenate to view the function description. Click "WSDL", or access the Web service file and add a query string to it "? WSDL "(http: // 127.0.0.1/nusoap/nusoap_server3.php? To obtain the WSDL content of the web service.

2.4.2 use WSDL to call Web Services

The structure of a program that calls a web service through WSDL is roughly the same as that of a Web service without using WSDL. The difference is that when you call a web service through WSDL and initialize the soapclient class, two parameters are passed into the soapclient constructor. The first parameter is the address of the WSDL file, and the second parameter specifies whether to use the WSDL, set this parameter to true. See the following code. The code file name is "/nusoap/nusoap_client3.php"

 

<? Phprequire_once ("lib/nusoap. php ");

$ Client = new soapclient ('HTTP: // 127.0.0.1/nusoap/nusoap_server3.php? WSDL ', true );

$ Parameters = array ('string 1', 'string 2 ');

$ STR = $ client-> call ('concatenate', $ parameters );

If (! $ Err = $ client-> geterror ()){

Echo "program return:", $ STR;

} Else {

Echo "error:", $ err;

}

?>

 

2.4.3 use of proxy

Nusoap provides proxy methods to call remote web services. In this way, create a remote service proxy object in the client program and call the remote web service directly through the proxy, instead of using the call method of the soalclient class. See the following code.

 

<? Phprequire_once ("lib/nusoap. php ");

$ Client = new soapclient ('HTTP: // 127.0.0.1/nusoap/nusoap_server3.php? WSDL ', true );

$ Proxy = $ client-> getproxy (); // create a proxy object (soap_proxy class)

$ STR = $ proxy-> concatenate ("parameter 1", "parameter 2"); // directly call the Web Service

If (! $ Err = $ proxy-> geterror ()){

Echo "program return:", $ STR;

} Else {

Echo "error:", $ err;

}

?>

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.