Ec (2); NuSoap is a WebService programming tool in the PHP environment, used to create or call WebService. It is an open-source software. It is a series of PHP classes that use PHP language to send and receive SOAP messages over HTTP. It is developed by NuSphereCorporation (www.111cn.net. One advantage of NuSOAP is that it does not require the support of extension libraries. This feature allows NuSoap to be used in all PHP environments, not script ec (2); script
NuSoap is a WebService programming tool in the PHP environment. It is used to create or call WebService. It is an open source software, is fully written in PHP language, through HTTP to send and receive SOAP messages of a series of PHP class, developed by NuSphere Corporation (http://www.111cn.net. One advantage of NuSOAP is that it does not require the support of extension libraries. This feature allows NuSoap to be used in all PHP environments and is not affected by server security settings.
Method 1: directly call
Include ('nusoap. php ');
// Create a soapclient object whose parameter is server's WSDL
$ Client = new soapclient ('HTTP: // localhost/Webservices/Service. asmx? WSDL ', 'wsdl ');
// Input parameters in array format
$ AryPara = array ('strusername' => 'username', 'strpassword' => MD5 ('Password '));
// Call a remote Function
$ AryResult = $ client-> call ('login', $ aryPara );
// Echo $ client-> debug_str;
$ Document = $ client-> document;
Echo <
$ Document
SoapDocument;
?>
Method 2: proxy call
Require ('nusoap. php ');
// Create a soapclient object whose parameter is server's WSDL
$ Client = new soapclient ('HTTP: // localhost/Webservices/Service. asmx? WSDL ', 'wsdl ');
// Generate proxy class
$ Proxy = $ client-> getProxy ();
// Call a remote Function
$ AryResult = $ proxy-> login ('username', MD5 ('Password '));
// Echo $ client-> debug_str;
$ Document = $ proxy-> document;
Echo <
$ Document
SoapDocument;
?>
Many friends who use NuSoap to call. NET WebService or J2EE WebService may have encountered Chinese garbled characters. The following describes the causes and solutions of this problem.
Reasons for garbled WebService calling by NuSoap:
Usually we do WebService development is used for UTF-8 encoding, then we need to set:
$ Client-> soap_defencoding = 'utf-8 ′;
At the same time, xml must be transmitted in the same encoding mode:
$ Client-> xml_encoding = 'utf-8 ′;
At this point, everything is normal, but when we output the results, we find that the returned code is garbled.
Solution to WebService call garbled by NuSoap:
In fact, a friend who has enabled the debugging function will find that $ client-> response returns the correct result. Why $ result = $ client-> call ($ action, array ('parameters '=> $ param); is it garbled?
After studying the NuSoap code, we will find that when xml_encoding is set to UTF-8, NuSoap will detect the decode_utf8 setting. If it is true, it will execute the utf8_decode function in PHP, nuSoap is set to true by default. Therefore, we need to set:
$ Client-> soap_defencoding = 'utf-8 ′;
$ Client-> decode_utf8 = false;
$ Client-> xml_encoding = 'utf-8 ′;
Additional information
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/, where you can download to the latest version of NuSOAP.
The installation of NuSOAP is relatively simple. Copying the downloaded NuSOAP file to the server can be stored in an independent directory or in the same directory as the program code, 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. Among them, the lib directory stores all the source code files of NuSOAP, and the samples directory contains some examples provided by the NuSOAP development team. 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 class soap_server is used to create WEB Services, and the class soapclient 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
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 in post mode 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
Require_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 '); // call the WEB service program using the call method of the client object
$ Str = $ client-> call ('hello'); // The getError () method of the client object can be used to check whether an error occurs during the call.
// 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:
Require_once ("lib/nusoap. php ");
Function concatenate ($ str1, $ str2 ){
If (is_string ($ str1) & is_string ($ str2 ))
Return $ str1. $ str2;
Else
Return new soap_fault ('client', ", 'parameters 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:
Require_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:
Require_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'
';
Echo 'request :';
Echo'
’,htmlspecialchars($client->request,ENT_QUOTES),’
';
Echo 'response :';
Echo'
’,htmlspecialchars($client->response,ENT_QUOTES ),’
';
?>
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.
Require_once ("lib/nusoap. php ");
Function concatenate ($ str1, $ str2 ){
If (is_string ($ str1) & is_string ($ str2 ))
Return $ str1. $ str2;
Else
Return new soap_fault ('client', ", 'parameters 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 ".
Require_once ("lib/nusoap. php ");
Function concatenate ($ str1, $ str2 ){
If (is_string ($ str1) & is_string ($ str2 ))
Return $ str1. $ str2;
Else
Return new soap_fault ('client', ", 'parameters 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") // definition of the return Parameter
);
$ 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
View the WSDL for the service. Click on an operation name to view it's details.
Concatenate
Click the function name concatenate to view the function description. Click "WSDL" or access the WEB service file and add the query string "? 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"
Require_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.
Require_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;
}
?>