Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML declaration not playing nice with PHP

Tags:

php

xml

quotes

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.

like image 425
esqew Avatar asked Nov 13 '22 16:11

esqew


1 Answers

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 :)

like image 145
dnshio Avatar answered Dec 30 '22 22:12

dnshio