如何阻止 ZF 在开头发送空字符?
我正在使用 ZF 开发一个具有 REST API 的应用程序。一切都很顺利,只是我的 XML 在开头有一个空白字符,因此 XML 违反了在开头有 XML 声明的规则。我正在尝试使用 javascript/jquery 来使用它,并且在 firebug 的控制台上收到以下错误:
XML 解析错误:XML 或文本声明不在实体开头
有许多关于 SO 和网络上其他地方的帖子讨论了这个问题。这是由于输出缓冲,我已经按照其他地方的建议尝试使用 ob_flush 和 flash,但我只是不知道如何做到这一点,因为这些帖子没有提到这些应该去哪里。也许我没有正确理解输出缓冲,但是谁能告诉我到底需要在哪里放置这些调用来删除这个空白字符?
我真的很感激您的回复,因为我现在已经花了好几个小时在这上面(我花了很长时间才弄清楚这首先是问题所在),而且我对此感到疯狂!哈哈!
谢谢
I'm developing an app using ZF which has a REST API. Everything is going well except that my XML has a blank character at the beginning and so the XML is breaking the rules of having the XML declaration at the beginning. I'm trying to consume this using javascript/jquery and I'm getting the following error on firebug's console:
XML Parsing Error: XML or text declaration not at start of entity
There are a number of posts on SO and other places on the web which discusses this. It is due to output buffering and I've tried using ob_flush and flush as suggested elsewhere but I just can't figure out how to do it as those posts don't mention where these should go. Maybe I don't understand output buffering properly but Can anyone tell me where exactly I need to put these calls to remove this blank character?
I'd really appreciate a response as I've spent a good few hours on this now (took me quite a while to figure out that this was the problem in the first place) and I'm going barmy over it! LOL!
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能已将 PHP 文件保存为带有 字节顺序标记 (BOM) 编码为 UTF-8 的文件。尝试保存编码为 UTF-8 且不带 BOM 的 PHP 文件。高级文本编辑器(例如 Windows 上的 Notepad++、Mac OS 上的 TextWrangler)或大多数其他高级文本编辑器都具有此类功能。
另一种可能性是,您的 PHP 文件中的任何
之前有无意的空格字符,例如在单个 PHP 文件的示例中:
...或您的 PHP 文件输出的其他无意的空格字符PHP 代码。
如果您已经解决了这些问题,那么就不需要输出缓冲了。使用输出缓冲作为规避此类问题的手段是一种草率的编码习惯。
编辑:
想一想,它也可能解决不了问题。
You probably have saved your PHP files encoded as UTF-8 with a Byte Order Mark (BOM). Try saving your PHP files encoded as UTF-8 without a BOM. Advanced text-editors such as Notepad++ on Windows, or TextWrangler on Mac OS, or most other advanced text-editors have such a feature.
Another possibility is that you have unintentional whitespace characters before the any
<?php
in your PHP files, for instance in this example of a single PHP file:...or other unintentional whitespace characters outputted by your PHP code.
If you've taken care of these sorts of problems, then there is no need for output buffering. Using output buffering as a means to circumvent these types of problems is a sloppy coding habit.
edit:
Nor would it probably solve the problem, come to think of it.
你可以简单地做到这一点:)
you can simpy do this :)