问题描述:
使用 nosoap_client 类创建了一个客户端 SOAP 对象,然后从 $client->call 方法传回UTF-8编码的中文数据时,所有的中文数据均显示为问号(nusoap 版本为 0.9.5)
问题原因:
nusoap_client 的 decode_utf8 属性默认设置为 true,导致默认对UTF-8数据进行转码,在 nusoap 中的具体调用过程如下:
class.soapclient.php 中的 nusoap_client 的 parseResponse 在解析返回的数据时使用了 nusoap_parser 类
$parser = new nusoap_parser($data, $this->xml_encoding, $this->operation, $this->decode_utf8);
class.soap_parser.php 中 soap_parser 类的 character_data 方法中又调用了utf8_decode进行了转码
if ($this->xml_encoding=='UTF-8'){
// TODO: add an option to disable this for folks who want
// raw UTF-8 that, e.g., might not map to iso-8859-1
// TODO: this can also be handled with xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, "ISO-8859-1");
if($this->decode_utf8){
$data = utf8_decode($data);
}
}
解决方法:
在使用 $client = new nusoap_client() 创建了对象之后马上设置 decode_utf8 为 false;
设置方法如下:
$client->decode_utf8 = false;
或
$client->decodeUTF8(false);
补充:
又遇到一则因服务端输出信息编码不匹配,导致 NuSOAP XML Parser 以’ISO-8859-1’编码解析而出错的问题,NuSOAP 库相关部分代码在 class.nusoap_base.php 中,具体位置如下:
/**
* charset encoding for outgoing messages
*
* @var string
* @access public
*/
var $soap_defencoding = 'ISO-8859-1';
//var $soap_defencoding = 'UTF-8';
解决方法:
$client->soap_defencoding = 'utf-8'; # 仅比上述问题增加该项设置 $client->xml_encoding = 'utf-8'; # 默认值 $client->decode_utf8 = false;
参考链接:
http://blog.csdn.net/mynamesucks/archive/2006/05/26/756480.aspx
http://lhx1026.javaeye.com/blog/506092
http://plog.longwin.com.tw/programming/2010/03/16/php-soap-nusoap-client-2010

0 条评论。