Home  >  Article  >  Backend Development  >  What to do if php webservice is garbled?

What to do if php webservice is garbled?

藏色散人
藏色散人Original
2020-08-28 09:51:011808browse

Solution to php webservice garbled code: First open the "nusoap.php" file; then modify the content to "var $soap_defencoding = 'UTF-8';" and "var $decode_utf8 = false;".

What to do if php webservice is garbled?

Recommended: "PHP Video Tutorial"

php calls webservice Chinese garbled solution

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. Use the example it comes with to modify it. You can find the download address in the attachment at the end of the article. The code is as follows:

<?php 
require_once(&#39;../nusoap/lib/nusoap.php&#39;); 
$client = new soapclient(&#39;http://localhost/TestService/Service1.asmx?WSDL&#39;, true); 
$err = $client->getError(); 
if ($err) { 
echo &#39;<h2>Constructor error</h2><pre class="brush:php;toolbar:false">&#39; . $err . &#39;
'; } // 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

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

Error

&#39; . $err . &#39;
'; } else { // Display the result echo '

Result

&#39;; 
  print_r($result); 
  echo &#39;
'; } } echo '

Request

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

Response

&#39; . htmlspecialchars($client->response, ENT_QUOTES) . &#39;
';//开源代码最模板zuimoban.com echo '

Debug

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

If there is no Chinese in the content returned by my webservice, it is very simple to use the above example. It was successful. But a new problem appeared. If there is Chinese in my webservice, the returned value becomes garbled. I checked the response result and it is correct. It should have appeared when processing it with nusoap. Question.

Because I have not installed the PHP breakpoint debugging tool (in fact, I don’t even know if there is such a tool), so I can only look at one method and one method. After looking at it for a day, I finally finished it. I changed nusoap .php is OK.

Changed places:

86行的:var $soap_defencoding = &#39;UTF-8&#39;;
4998行的:var $decode_utf8 = false;

It’s okay not to change, but you must specify the encoding when calling.

$client = new soapclient(&#39;http://localhost/TestService/Service1.asmx?WSDL&#39;, true); 
$client->soap_defencoding = &#39;UTF-8&#39;; 
$client->soap_defencoding = &#39;UTF-8&#39;;

Like this 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 the parameters with Chinese characters in the code, you need to convert the encoding to UTF8. You may refer to the attachment. .

The above is the detailed content of What to do if php webservice is garbled?. For more information, please follow other related articles on the PHP Chinese website!

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