Nusoap WSDL File Description:
My server is created as follows:
App::import (' Vendor ', ' nusoap/lib/nusoap ');
$server = new Soap_server;
$server->configurewsdl (' Sum ');
$server->register (' Sum ', Array (' x ' = ' xsd:string ', ' y ' = ' xsd:string '), Array (' return ' = ' xsd:string '), ' ', ' http://localhost/ws/info/sum ');
$HTTP _raw_post_data = isset ($HTTP _raw_post_data)? $HTTP _raw_post_data: ";
$server->service ($HTTP _raw_post_data);
$this->set (' data ', $server);
$this->autorender = false;
Enter in the browser
http://localhost/ws/info?wsdl
The return XML message contains this sentence
What is the value of soap:address location we can set when creating $server, because the use of cakephp with default values will be an error.
------Solution--------------------
Give a reference example:
PHP Call WebService instance, read it to understand
Nusoap is a WebService programming tool for creating or invoking webservice in a PHP environment. It is an open source software, developed by Nusphere Corporation (http://dietrich.ganx4.com/nusoap/), a series of PHP classes written entirely in PHP that send and receive SOAP messages over HTTP. One advantage of Nusoap is that there is no need for extended library support, which allows Nusoap to be used in all PHP environments and is not affected by server security settings.
Many friends who use Nusoap to call. NET WebService or Java EE WebService may have encountered Chinese garbled problems, and the following describes the causes of this problem and the corresponding solutions.
Nusoap Call WebService garbled reason:
Usually when we do webservice development, we use UTF-8 encoding, which we need to set:
$client->soap_defencoding = ' utf-8′;
At the same time, the XML needs to be passed in the same encoding:
$client->xml_encoding = ' utf-8′;
This should be all right, but when we output the results, but found that the return is garbled.
Nusoap Call WebService the solution that appears garbled:
In fact, the debugging function of the friend, I believe will find $client->response return is the correct result, why $result = $client->call ($action, Array (' Parameters ' = $ param)); But it's garbled?
After studying the NUSOAP code we will find that when xml_encoding is set to UTF-8, Nusoap detects the Decode_utf8 setting, and if true, executes the Utf8_decode function inside PHP, and Nusoap defaults to True , therefore, we need to set:
Nusoap is a Web services programming tool for creating or invoking Web services in a PHP environment. It is an open source software, the current version is 0.7.2, supports SOAP1.1, WSDL1.1, can interoperate with other systems that support SOAP1.1 and WSDL1.1. Nusoap is written entirely in PHP and consists of a series of PHP classes that do not require extended library support, which makes nusoap available to all PHP environments and is not affected by server security settings.
1. Acquisition and installation of Nusoap
The Nusoap project is built on SourceForge, the network address is: http://sourceforge.net/projects/nusoap/, here, can be downloaded to the latest version of Nusoap.
Nusoap installation is relatively simple, the download of nusoap files copied to the server, can be placed in a separate directory, you can also be in the same directory with the program code, as long as your PHP code can access these files.
The test environment for this article is based on PHP4.3.2 and Nusoap 0.7.2, Nusoap installed in the WEB directory "/nusoap", with two subdirectories, Lib and samples. Where the Lib directory contains all the source code files of Nusoap, samples directory is the NUSOAP development team to provide some examples. The test file is stored in the WEB directory "/nusoap".
2. Use of Nusoap
Nusoap consists of a class of PHP, the most commonly used are class Soap_server and class Soalclient. Class Soap_server is used to create a Web service that is used by class soapclient when accessing a Web service.
2.1 A simple example: Hello world
This example will use Nusoap to create a simple WEB service and use Nusoap to create a client program that invokes the 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 Nusoap source file in the current code file.
Require_once ("lib/nusoap.php");
Defining a service Program
function Hello () {
Return ' Hello world! ';
}
Initializes the service object, which is an instance of the class Soap_server
$soap = new Soap_server; The Register method that invokes the service object registers the program that needs 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 serving object.
The service method processes the input data, invokes the corresponding function or method, and generates the correct feedback, which is passed back to the client.
$soap->service ($HTTP _raw_post_data);
?> so far, the Web service program code file has been built, next, create a client program code file "/nusoap/nusoap_client1.php", call the Web service:
Include the Nusoap source file in the current code file.
Require_once ("lib/nusoap.php");
Initializes the client object, which is an instance of the class SoapClient,
Pass the URL address of the service program to the constructor of the SoapClient class.
$client = new SoapClient (' http://127.0.0.1/nusoap/nusoap_server1.php '); Invoking a program for a WEB service using the call method of a client object
$str = $client->call (' hello '); The GetError () method of the client object can be used to check for errors in the calling procedure.
If there is no error, the GetError () method returns False, and if there is an error, the GetError () method returns an error message.
if (! $err = $client->geterror ()) {
echo "program returns:", Htmlentities ($str, ent_quotes);
} else {
echo "Error:", Htmlentities ($err, ent_quotes);
}
?> so far, the client program is also established, open the browser, access the client program, look at the results. In this example, the browser will display the string: "program return: Hello world! ”
2.2 Methods for passing parameters and returning error messages
The method of passing parameters and returning error information is illustrated by examples. This example implements the connection of two strings, the argument is two strings, and the return value is a string that is concatenated 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 ($str 1, $str 2) {
if (is_string ($str 1) && is_string ($str 2))
Return $STR 1. $STR 2;
Else
return new Soap_fault (' Client ', ', ' The parameter of the ' concatenate ' function should be two strings ');
}
$soap = new Soap_server;
$soap->register (' concatenate ');
$soap->service ($HTTP _raw_post_data);
?> compared to the Code of section 2.1 WEB service programs, the code structure is basically the same. Note the following two points:
The service program is defined differently, with two parameters. Nusoap the process of registering a service program is the same as the Register method that invokes the service object.
A new class Soap_fault for Nusoap is used here. When an incoming two parameter has a not a string, the program returns the error message to the client through this class. The constructor for this class has 4 parameters:
Fault
Code
Required parameter, the recommended value is "client" or "Server", indicating whether the error is a client error or a service-side error.
Faultactor
Reserved items that are not yet used
FaultString
Incorrect description information
Faultdetail
Optional, XML-formatted data that describes detailed error information
The complete contents of the client program code file "/nusoap/nusoap_client2.php" are 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 returns:", $str;
} else {
echo "Error:", $err;
}
?> the Nusoap client invokes a WEB service with parameters, the array is used to pass the parameters. $parameters is an array in which the values of each parameter are sequentially. When a client invokes a remote service program, it uses a call method with two parameters, the first parameter is the name of the service program, and the second parameter is an array of parameters for the service program, here is $parameters. Through the browser to access the above client program, the browser will display a string: "program return: String 1 string 2"
Next, try to pass the error parameter to the WEB service program, modify the client program above, change the statement that generates the parameter array to: $parameters =array ("string", 12), and then through the browser to access the client program, the browser will display the string: "Error: Client: The parameters of the CONCATENATE function should be two strings ". The WEB service program determines that an incoming parameter has one that is not a string and returns an error message to the client through Soap_fault.
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.