实体“acirc”;不是从 simplexml_load_string 定义的
所以我有一个 PHP 脚本,用于解析来自 Google Analytics 的 XML 响应。它已经工作了几个星期,但今天我收到了这个警告,并且简单的 xml 加载字符串没有将 xml 响应转换为 simplexml 对象。
消息:simplexml_load_string(): 实体:第 743 行:解析器错误: 实体“acirc”未定义
在将响应传递到 simplexml_load_string 之前,我已尝试对响应进行 utf8_encoding,但它不起作用。我相信 acirc 是一个编码符号,并且这个符号以某种方式搞砸了解析器?
我收到上面的警告两次,然后我也收到这个警告两次:
消息:simplexml_load_string(): 实体:第 743 行:解析器错误: 实体“cent”未定义
任何建议都会有很大的帮助,谢谢!
感谢 Marc 的评论,我已经尝试过这个:
$xml = simplexml_load_string(htmlspecialchars_decode($response_body));
我收到一个有趣的错误,但 xml 对象仍然是空的:
实体:第 743 行:解析器错误:输入 不是正确的UTF-8,指示编码 !字节:0x84 0x26 0x63 0x65
So I have a PHP Script thats parsing an XML Response from Google Analytics. It has been working for a few weeks, but today I've been getting this warning, and simple xml load string is not converting the xml response into a simplexml object.
Message: simplexml_load_string():
Entity: line 743: parser error :
Entity 'acirc' not defined
I have tried utf8_encoding the response before I pass it into simplexml_load_string, but it isn't working. I believe that acirc is an encoded symbol, and that this symbol is screwing up the parser somehow?
I get the warning above twice, and then I get this one twice also:
Message: simplexml_load_string():
Entity: line 743: parser error :
Entity 'cent' not defined
Any advice would be a huge help, thanks!
Thanks for the comment Marc, I have tried this:
$xml = simplexml_load_string(htmlspecialchars_decode($response_body));
And I get an interesting error, but the xml object is still empty:
Entity: line 743: parser error : Input
is not proper UTF-8, indicate encoding
! Bytes: 0x84 0x26 0x63 0x65
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要从中创建 SimpleXMLElement 的字符串不是有效的 XML。简单的 xml 函数只能处理有效的 XML。对于任何无效的内容,您都会收到错误,并且不会出现任何元素。当您处理数据时,您已经看到了一些错误。
第一个错误只是您的 XML 具有未定义实体的信息。由于我不明白的原因,您发现特别有趣的第二个错误是因为您确实将字符串抛弃到了某种编码涅槃中。因此,即使字符串也不再被 simplexml 正确编码来处理。
如果您得到的响应确实是有效的 XML,则向 google 提交错误报告,让他们修复损坏的输出,然后您的问题应该得到解决。
编辑:
您从浏览器复制并粘贴它?如果是这样,请尝试在将其加载为 XML 之前对其运行
html_entity_decode()
。可能您加载的不是 XML,而是 HTML。最好使用DomDocument
和loadHTML
函数,这样就不需要解码任何内容。The string you want to create a SimpleXMLElement from is not valid XML. The simple xml function can only deal with valid XML. For anything invalid you'll get errors and no element instead. As you played around with the data already you've seen some of the errors.
The first error is just the information that your XML has undefined entities. The second error you find especially interesting for a reason I don't understand is because you really ditched the string into some encoding nirvana. So then even the string isn't properly encoded any longer for simplexml to deal with.
If that response you get should really be valid XML, then file a bugreport with google, make them fix their broken output and then your issue should be solved.
Edit:
You copy and paste it from the browser? If so try running
html_entity_decode()
on it before loading it as XML. Probably you're not loading XML but HTML. Better useDomDocument
and theloadHTML
function instead, no need to decode anything then.