XML declaration not playing nice with PHP
I'm using PHP to output custom XML based upon values stored in a MySQL database. I have 开发者_运维知识库the following at the start of the condition:
echo '<?xml version="1.0" encoding="UTF-8"?>';
(I've also tried this variation:
echo "<?xml version='1.0' encoding='UTF-8'?>";
to no avail.)
However, this is always outputted (in every browser and even when viewed as a downloaded file from wget
) as:
<?xml version=1.0 encoding=UTF-8 ?>
Somewhere along the line, the single quotes around the version number and encoding got dropped. Where did I go wrong and what can I do to fix this? I'm getting errors from my parser and I believe it's because this declaration is not following the XML standard of having either single or double quotes around these values.
I think this is because the content type of your document is already set to text/html (by default by php).
The solution to this is to set this as a header rather than echo it.
eg :
header ("Content-Type:text/xml");
to set the charset :
header('Content-Type: text/xml; charset=utf-8');
And make sure that you set this BEFORE you output (echo) anything else.
Hope that helps :)
精彩评论