Home  >  Article  >  Backend Development  >  PHP calls webservice Chinese garbled solution_PHP tutorial

PHP calls webservice Chinese garbled solution_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:09:54882browse

Due to work needs, I helped my colleague A Tao test the application of .net webservice in PHP. So I started to search for information on the Internet and found that many PHP tutorials use nusoap. So I downloaded one and used it myself Modify the example. You can find the download address in the attachment at the end of the article.
The code is as follows:

require_once('. ./nusoap/lib/nusoap.php');
$client = new soapclient('http://localhost/TestService/Service1.asmx?WSDL', true);
$err = $client-> ;getError();
if ($err) {
echo '

Constructor error

' . $err . '
';
}
// Doc/lit parameters get wrapped
$param = array('str' => 'China');
$result = $client->call('HelloWorld', array(' parameters' => $param), '', '', false, true,'document','encoded');
// Check for a fault
if ($client->fault) {
echo '

Fault

';<br>print_r($result);<br>echo '
';
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error
echo '

Error' . $err . '';
} else {
// Display the result
echo '

Result

 ';<br> print_r($result);<br> echo '
';
}
}
echo '

Request

' . htmlspecialchars($client->request, ENT_QUOTES) . '
';
echo '

Response

' . htmlspecialchars($client->response, ENT_QUOTES) . '
';
echo '

Debug

' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '
' ;
?>


If there is no Chinese in the content returned by my webservice, the above example will be successful. But a new problem arises, If there is Chinese in my webservice, the returned value will become garbled. I checked the response result and it is correct. There should be a problem when processing it with nusoap.
Because I did not install PHP breakpoints Debugging tool (actually I don’t even know if there is such a tool). So I can only look at one way and one way. After looking at it for a day, I finally finished it. I changed two places in nusoap.php and it was OK.
Changed places :
Line 86: var $soap_defencoding = 'UTF-8';
Line 4998: var $decode_utf8 = false;
================ ================================================== =====
You can leave it unchanged, but you must specify the encoding when calling
$client = new soapclient('http://localhost/TestService/Service1.asmx?WSDL', true);
$client->soap_defencoding = 'UTF-8';
$client->soap_defencoding = 'UTF-8';
The effect is the same.
================================================== ========================
If the parameters in the request are in Chinese, just send them by post or get.
If you need to write parameters with Chinese characters in the code, you need to convert the encoding to UTF8. You may refer to the attachment.

Attachment:
nusoap source file: http:/ /www.cnblogs.com/Files/coolstr/nusoap.zip
Various encoding conversion source files: http://www.cnblogs.com/Files/coolstr/chinese.zip


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444768.htmlTechArticleDue to work needs, I helped my colleague A Tao test the application of .net webservice under PHP. So I started looking for information online and found that many PHP tutorials used nusoap. So I downloaded one...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn