SOAP protocol invocation WebService and Nusoap java/c# WebService Chinese garbled problem

Source: Internet
Author: User
Tags soap md5 php code wsdl

Method One: Direct call

The code is as follows Copy Code
?


Include (' nusoap.php ');

Creates a SoapClient object, which is the WSDL of the server
$client = new SoapClient (' Http://localhost/Webservices/Service.asmx?WSDL ', ' WSDL ');

parameter to the array form
$aryPara = Array (' strUserName ' => ' username ', ' strpassword ' =>md5 (' password '));

Calling remote functions
$aryResult = $client->call (' login ', $aryPara);

Echo $client->debug_str;


$document = $client->document;
Echo <<<soapdocument
<?xml version= "1.0″encoding=" gb2312″?>
<soap-env:envelope soap-env:encodingstyle= "http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap-env= "http:// schemas.xmlsoap.org/soap/envelope/"xmlns:xsd=" Http://www.w3.org/2001/XMLSchema "xmlns:xsi=" http://www.w3.org/ 2001/xmlschema-instance "xmlns:soap-enc=" http://schemas.xmlsoap.org/soap/encoding/"xmlns:si=" http:// Soapinterop.org/xsd ">
<SOAP-ENV:Body>
$document
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Soapdocument;

?>

Method Two: Proxy method call

The code is as follows Copy Code
?


Require (' nusoap.php ');

Creates a SoapClient object, which is the WSDL of the server
$client =new soapclient (' Http://localhost/Webservices/Service.asmx?WSDL ', ' WSDL ');

Generate proxy class
$proxy = $client->getproxy ();

Calling remote functions
$aryResult = $proxy->login (' username ', MD5 (' password '));

Echo $client->debug_str;


$document = $proxy->document;
Echo <<<soapdocument
<?xml version= "1.0″encoding=" gb2312″?>
<soap-env:envelope soap-env:encodingstyle= "http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap-env= "http:// schemas.xmlsoap.org/soap/envelope/"xmlns:xsd=" Http://www.w3.org/2001/XMLSchema "xmlns:xsi=" http://www.w3.org/ 2001/xmlschema-instance "xmlns:soap-enc=" http://schemas.xmlsoap.org/soap/encoding/"xmlns:si=" http:// Soapinterop.org/xsd ">
<SOAP-ENV:Body>
$document
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Soapdocument;

?>



Nusoap Call WebService the reason for garbled:

Usually we use the UTF-8 code for WebService development, when we need to set:

The code is as follows Copy Code

$client->soap_defencoding = ' utf-8′;

At the same time, XML needs to be passed in the same encoding:

The code is as follows Copy Code

$client->xml_encoding = ' utf-8′;

At this point should be all normal, but we are in the output of the results, but found that the return of garbled.

Nusoap Call WebService a garbled solution:

In fact, the friend that turned on the debugging function, I believe will find $client->response return is the correct result, why

The code is as follows Copy Code

$result = $client->call ($action, Array (' Parameters ' => $param));

But it's garbled?


After studying the NUSOAP code, we find that when xml_encoding is set to UTF-8, Nusoap detects Decode_utf8 settings, and if true, executes the Utf8_decode function in PHP, and Nusoap defaults to True , so we need to set:


PHP Code

  code is as follows copy code
$client-> soap_defencoding = ' utf-8′;   
$client->decode_utf8 = false;   
$client- >xml_encoding = ' utf-8′;   
Related Article

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.