【问题标题】:PHP XML UTF-8 with special characters throws errors带有特殊字符的 PHP XML UTF-8 会引发错误
【发布时间】:2012-01-30 04:25:13
【问题描述】:

我在从 DHL API 接收 UTF-8 XML 文件时遇到一些问题。只要我不发送任何特殊字符(如 ś 或 ó),一切正常,但使用这些字符时,我的应用程序在尝试加载从 DHL 接收的 XML 文件时崩溃并抛出错误:

Warning:  DOMDocument::loadXML() [domdocument.loadxml]: 
Opening and ending tag mismatch: AddressLine line 43 and Consignee 
in Entity, line: 53 in D:\xampp\htdocs\ebay\catch2.php on line 29

Warning:  DOMDocument::loadXML() [domdocument.loadxml]: 
Opening and ending tag mismatch: Consignee line 40 and res:ShipmentValidateResponse 
in Entity, line: 97 in D:\xampp\htdocs\ebay\catch2.php on line 29

Warning:  DOMDocument::loadXML() [domdocument.loadxml]: Premature end of 
data in tag ShipmentValidateResponse line 1 in Entity, line: 98
in D:\xampp\htdocs\ebay\catch2.php on line 29

这是我发送的 XML

<?xml version="1.0" encoding="utf-8"?>
... 
<AddressLine>address</AddressLine> 
<AddressLine>asfśó</AddressLine> 
...

我收到了什么:

<?xml version="1.0" encoding="UTF-8"?>
...
Lines 40 to 43:

<Consignee>
<CompanyName>Person</CompanyName>
<AddressLine>address</AddressLine>
<AddressLine>asf??ddressLine>
...

这是第 29 行附近发生的情况:

$responseXml = $session->sendHttpRequest($requestXmlBody);
if(stristr($responseXml, 'HTTP 404') || $responseXml == '')
    die('<P>Error sending request');
$responseXml = utf8_decode($responseXml);
$responseDoc = new DOMDocument('1.0', 'UTF-8');
$responseDoc->loadXML($responseXml);

E: 删除 utf8_decode 并没有多大帮助。只是一个新错误:

Warning:  DOMDocument::loadXML() [domdocument.loadxml]: 
Input is not proper UTF-8, indicate encoding !
Bytes: 0xF3 0x3C 0x2F 0x41 in Entity, line: 43 in D:\xampp\htdocs\ebay\catch2.php on line 29

E2: 十六进制转储

0000-0010:  3c 3f 78 6d-6c 20 76 65-72 73 69 6f-6e 3d 22 31  <?xml.ve rsion="1
0000-0020:  2e 30 22 20-65 6e 63 6f-64 69 6e 67-3d 22 55 54  .0".enco ding="UT
0000-0030:  46 2d 38 22-3f 3e 3c 72-65 73 3a 53-68 69 70 6d  F-8"?><r es:Shipm

第 43 行:

0000-0960:  4c 69 6e 65-3e 0a 20 20-20 20 20 20-20 20 3c 41  Line>... ......<A
0000-0970:  64 64 72 65-73 73 4c 69-6e 65 3e 61-73 66 3f f3  ddressLi ne>asf?.
0000-0980:  3c 2f 41 64-64 72 65 73-73 4c 69 6e-65 3e 0a 20  </Addres sLine>..

【问题讨论】:

  • 嗯,这些字符的编码确实不是UTF-8。选项 1:您没有向服务器发送 UTF-8,服务器只是按原样返回字节,选项 2:API 中存在错误。
  • 这可能是选项1。但是我如何发送 utf-8?第一行 "" 还不够吗?我可以看到他们的 API 收到了我的请求。
  • &lt;?xml version="1.0" encoding="utf-8"?&gt; 只是通知收件人它应该期望文件的内容是 UTF-8 编码的。这对文件的实际编码没有任何作用,you 仍然负责实际交付 UTF-8 编码的内容。我建议您阅读What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text,然后尝试找出您的内容使用哪种编码。
  • 好的,谢谢。现在可以工作了。使用 incov 将我的数据编码为 UTF-8。

标签: php xml utf-8


【解决方案1】:

不要使用utf8_decode

这就是你的编码搞砸了。
utf8_decode 将 UTF-8 编码的文本转换为 Latin1 编码的文本。这不是你想要或需要的。只需按原样解析 XML,无需编码转换。

【讨论】:

  • 作用不大,还是报错:Warning: DOMDocument::loadXML() [domdocument.loadxml]: Input is not proper UTF-8,指示编码!字节:Entity 中的 0xF3 0x3C 0x2F 0x41,行:D:\xampp\htdocs\ebay\catch2.php 第 29 行中的 43
  • 那么文件可能从一开始就不正确?您可以发布文档第一行和第 43 行的十六进制转储吗?
  • @user 想一想:也许 API 返回的字节与您发送它的字节相同,而没有实际编码或解码它们。在这种情况下,您可能一开始就没有发送 UTF-8。你有没有机会utf8_encoding你的价值观?
  • 不,没有 utf8_encoding。在第一篇文章中添加了转储,这里看起来像废话。
猜你喜欢
  • 2011-02-21
  • 2014-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-24
  • 2011-01-21
  • 2015-01-15
相关资源
最近更新 更多