问题
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.
回答1:
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');
回答2:
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
回答3:
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
回答4:
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()
回答5:
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');
});
回答6:
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
// Å
来源:https://stackoverflow.com/questions/8006274/soap-error-encoding-string-is-not-a-valid-utf-8-string