问题
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
回答1:
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:
<-- here for instance
<?php
/*
some code to generate $yourXml;
*/
?>
<-- or here
<?php
echo $yourXml;
?>
...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.
回答2:
echo trim($yourXml);
you can simpy do this :)
来源:https://stackoverflow.com/questions/6833186/how-do-i-stop-zf-from-sending-a-empty-character-at-the-beginning