Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line breaks in PHP xmlwriter document

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:

source

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?

like image 933
user101289 Avatar asked Aug 04 '15 04:08

user101289


1 Answers

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

like image 92
ficuscr Avatar answered Nov 13 '22 21:11

ficuscr