Usoap is an open source Soap tool in a PHP environment, which is a much more used tool.
In the UTF-8 environment, NUSOAP can work very well. However, when used in Chinese environment, Nusoap often have some problems that people can not solve.
In a recent project, the server was implemented with NUSOAP, supporting UTF-8 and GBK two character sets.
An error occurred when the client invoked the service with GBK: Charset from HTTP Content-type Us-ascii does not match encoding from XML declaration GBK, meaning that in the client's request, the HTTP Content-type character set is Us-ascii, and in the XML declaration of the SOAP request, the character set is GBK, and the two do not match. Check the request variable of the SOAP client, the value of HTTP Content-type is also GBK, how can it become us-ascii? It's a little confusing. So had to trace the source of Nusoap, found that nusoap in the processing of HTTP Content-type when the Us-ascii,iso-8859-1,utf-8 character set is the default to Us-ascii. The reason for this is that NUSOAP uses XML parser, and XML parser only supports these kinds of character sets. Therefore, when the client is called, the character set of the HTTP Content-type and the SOAP request should be replaced with iso-8859-1 when the GBK is used.
Later, when you encapsulate the client, you also encounter a similar problem. The client character set is declared as GBK, and the service side returns the SOAP call result when both HTTP Content-type and SOAP request declare the character set to GBK and the client does not get any value. View the response object of the SOAP client and find that the server is returned correctly. To solve this problem, we had to modify the server side to declare both the HTTP Content-type and the SOAP response character set as Iso-8859-1.
So when using Nusoap, you can use ISO-8859-1 instead when you encounter the GBK or GB2312 character set.
=============================================================================================
PHP Web Service Server side:
- <?php
- Header ("Content-type:text/html;charset=utf-8");
- Pull in the Nusoap code
- Require_once ('./lib/nusoap.php ');
- Define the method as a PHP function
- function Hello ($name) {
- return ' Hello! ‘ . $name;
- }
- Create the server instance
- $server = new Soap_server;
- $server->configurewsdl (' hellowsdl ', ' urn:hellowsdl ');
- $server->wsdl->schematargetnamespace = ' urn:hellowsdl ';
- Register the method to expose
- $server->register (' Hello ',
- Array (' name ' = 'xsd:string '),
- Array (' return ' = 'xsd:string '),
- ' Urn:hellowsdl ',
- ' Urn:hellowsdl#hello ',
- ' RPC ',
- ' Encoded ',
- ' Say Hello to Somebody '
- );
- Use the request to invoke the service
- $HTTP _raw_post_data = isset ($HTTP _raw_post_data)? $HTTP _raw_post_data: ";
- $server->service ($HTTP _raw_post_data);
- ?>
Client side:
- <?php
- Header ("content-type:text/html;charset=gb2312");
- Pull in the Nusoap code
- Require_once ('./lib/nusoap.php ');
- Create the Client instance
- $client = new SoapClient (' http://localhost/soapTest/helloService.php?wsdl ', true);
- Call the SOAP method
- $param = Array ("name" = "Andy");
- $result = $client->call (' Hello ', $param);
- Display the result
- Print_r ($result);
- if (! $err =$client->geterror ()) {
- Print_r ($result);
- Print (' </br> ');
- echo "program returns:", Htmlentities ($result, ent_quotes,gb2312);
- }
- else{
- echo "error:", Htmlentities ($result, ent_quotes,gb2312);
- }
- echo ' $client, request, ent_quotes,gb2312). ' </pre> ';
- echo ' $client, Response, ent_quotes,gb2312). ' </pre> ';
- echo ' $client, Debug_str, ent_quotes,gb2312). ' </pre> ';
- ?>
Java code:
Note: To use axis1.x, go to the official website do not download the Axis2. It seems that axis1.x and Axis2 are still very different, and the current axis1.x documents compare all points. These are the online searches.
If you need to invoke the Web Service using the Chinese parameter, you must use the ISO-8859-1 encoding parameter, and the returned response is decoded again. Do not use another encoding, there will be errors!
Java code
...
Java code
- Import Org.apache.axis.client.Service;
- <span style="color: #464646; Font-family:simsun; line-height:21px; Text-align:left; White-space:normal; Background-color: #ffffff; " >import org.apache.axis.client.call;</span>
- Public class WebServiceTest {
- public static void Main (string[] args) {
- String endpoint = "http://localhost/soapTest/helloService.php";
- //string endpoint = "http://testweb.dev.php/testWebService/testWebService.php";//This paragraph is the address just above
- Service service = new service ();
- Call call;
- try {
- Call = (call) Service.createcall ();
- Call.settargetendpointaddress (new Java.net.URL (endpoint));
- Call.setoperationname ("Hello");
- string param = new String ("Andy". GetBytes (),"iso-8859-1"); If this paragraph is not added, the Chinese parameter will be garbled
- //string param = new String ("Chinese");
- string s = (string) call.invoke (new object[] {param});
- s = new String (S.getbytes ("iso-8859-1")); If there is no conversion code, Chinese will be garbled
- System.out.println (s);
- } catch (Exception e) {
- //TODO auto-generated catch block
- E.printstacktrace ();
- }
- }
- }
Java calls PHP's Web Service (iii)