I've got an XML feed I've created using XMLWriter
. It works flawlessly in dev on a PHP 5.6 vagrant box. On the live server, running PHP 5.4 the feed fails to render with a message:
This page contains the following errors:
error on line 3 at column 6: XML declaration allowed only at the start of the document
If you view source it looks like this:
Somehow there are a couple lines being added into the XML document. The only difference between the servers is the PHP version (as far as I know).
Here's the first few lines of the XMLWriter code:
$xml = new XMLWriter();
$xml->openURI('php://output');
$xml->startDocument("1.0");
$xml->setIndent(true);
$xml->startElement("propertyList");
$xml->writeAttribute('date', date('Y-m-d-H:i:s'));
Any ideas how to get around this?
Quite a few changes from PHP 5.4 to 5.6... let alone changes in libxml...
First thing is obviously make sure there is no white space before opening <?php
tag or after a closing tag if used.
It would help if you can determine when the new lines are introduced (assume they are new lines... have you used something like a hex viewer?). Try writing to a temp location - want to determine if this occurs when serving the page or when xmlWriter is outputting.
Things that come to mind...
Perhaps be explicit about what the indetString
should be. $xml->setIndentString(" ");
Default encoding...? Maybe try and get that set. Would expect on opening xml tag... encoding="UTF-8"
. Use startDocument('1.0', 'utf-8');
and probably should be sending header like: header('Content-Type: application/xml; charset=UTF-8');
. Is your default_charset
UTF-8?
What other differences between the two environments? Things likeshort_open_tag
etc.
LIBXML_HTML_NOIMPLIED
? Changed around 5.4?Workaround:
Try a call ob_clean
before starting to write to the output stream.
Use trim
.
Upgrade the server, who wants to be on 5.4 these days :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With