SOAP-ERROR: Encoding: string ..不是一个有效的utf-8字符串



嗨,我有一个使用Zend框架构建的web服务。其中一个方法用于发送订单的详细信息。我遇到了一些编码问题。返回的值之一包含以下内容:

Jaime Torres Bodet #322-A Col. Lomas de Santa María

webservice返回以下错误:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Body>
      <SOAP-ENV:Fault>
         <faultcode>SOAP-ENV:Server</faultcode>
         <faultstring>SOAP-ERROR: Encoding: string 'Jaime Torres Bodet #322-A Col. Lomas de Santa Marxc3...' is not a valid utf-8 string</faultstring>
      </SOAP-ENV:Fault>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我该如何处理这个问题?

感谢

follow - up:问题是由于数据库截断了字符串。该字段被设置为VARCHAR(50),并且它正好在编码值的中间截断。

如何改变编码设置:

服务器:

$server = new SoapServer("some.wsdl", array('encoding'=>'ISO-8859-1')); // for 'windows-1252' too
客户:

$server = new SoapClient("some.wsdl", array('encoding'=>'ISO-8859-1')); // for 'windows-1252' too

…然后自动转换为UTF-8,我有类似的问题,所以这对我有帮助,所以测试了

今天我遇到了同样的问题——导致这个问题的代码是:

$request->Text = substr($text, 0, 40);

将substr更改为mb_substr似乎解决了这个问题:

$request->Test = mb_substr($text, 0, 40, 'utf8');

问题是í != i.在请求中使用之前尝试将字符串转换为UTF-8。它可能看起来像这样:

$string = iconv('windows-1252', 'UTF-8', $string);

见http://php.net/iconv

以上的答案促使我尝试:

// encode in UTF-8
$string = utf8_encode($string);

这也为我解决了错误。

参考:utf8_encode ()

我使用mb_convert_encoding和array_walk_recursive来遍历我的POST参数,命名为$params (array),修复了这样的问题。

也许这对你有用:

array_walk_recursive($params,function (&$item){
    $item = mb_convert_encoding($item, 'UTF-8');
});

我发现,在我的情况下,不是字符串的编码是问题,而是文件本身没有保存为UTF-8。即使用UTF-8编码显式保存也无济于事。

对我来说,插入一个UTF-8字符的注释是有效的// Å

最新更新