SOAP-错误:编码:字符串...不是有效的 utf-8 字符串

发布于 2024-12-13 15:34:16 字数 760 浏览 3 评论 0 原文

您好,我有一个使用 Zend Framework 构建的 Web 服务。其中一种方法旨在发送有关订单的详细信息。我遇到了一些编码问题。返回的值之一包含以下内容:

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

Web 服务返回以下错误:

<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 Mar\xc3...' is not a valid utf-8 string</faultstring>
      </SOAP-ENV:Fault>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我应该如何解决此问题?

谢谢


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

Hi I have a web service built using the Zend Framework. One of the methods is intended to send details about an order. I ran into some encoding issue. One of the values being returned contains the following:

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

The webservice is returning the following fault:

<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 Mar\xc3...' is not a valid utf-8 string</faultstring>
      </SOAP-ENV:Fault>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

How should I go about this problem?

Thanks


Followup: Problem was due to a truncated string by the database. The field was set to VARCHAR(50) and it truncated exactly in the middle of the encoded value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梅倚清风 2024-12-20 15:34:16

更改编码设置怎么样:

服务器:

$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,我遇到了类似的问题,所以这对我有帮助,因此经过测试

What about change the encoding settings:

SERVER:

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

CLIENT:

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

... then the conversion is done automatically to UTF-8, I had the similiar problem, so this helped me, so it is tested

∞琼窗梦回ˉ 2024-12-20 15:34:16

今天我遇到了同样的问题 - 导致该问题的代码是:

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

将 substr 更改为 mb_substr 似乎可以解决该问题:

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

Today I run into same problem - the code which caused that problem was:

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

changing substr to mb_substr seems to solve the issue:

$request->Test = mb_substr($text, 0, 40, 'utf8');
多像笑话 2024-12-20 15:34:16

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

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

请参阅 http://php.net/iconv

The problem is that í != i. Try to convert your string to UTF-8 before using in a request. It may look like that:

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

See http://php.net/iconv

耳根太软 2024-12-20 15:34:16

上面的答案引导我尝试:

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

这也解决了我的错误。

参考:utf8_encode()

The answers above lead me to try:

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

which also resolved the error for me.

Reference: utf8_encode()

笑梦风尘 2024-12-20 15:34:16

我使用 mb_convert_encodingarray_walk_recursive 走进我的 POST 参数,名为$params(数组)。

也许这对你有用:

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

I fixed a problem like this using mb_convert_encoding with array_walk_recursive to walk into my POST parameters, named $params (array).

Maybe this is useful for you:

array_walk_recursive($params,function (&$item){
    $item = mb_convert_encoding($item, 'UTF-8');
});
呆° 2024-12-20 15:34:16

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

对我来说,插入带有 UTF-8 字符的注释很有效,例如
// Å

I found out that in my case not the encoding of strings was the problem but that the file itself was not saved as UTF-8. Even explicit saving with UTF-8 encoding did not help.

For me it worked to insert a comment with an UTF-8 character like
// Å

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文