7

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?

user101289
  • 9,888
  • 15
  • 81
  • 148
  • 2
    It's not from the code you've posted :) - And for the differences between the two servers compare the ini-settings, for example those about output buffering, prepend files etc.. And do you have the code under version control? – hakre Aug 09 '15 at 20:35
  • If the XML files you're generating aren't enormous you could always put the output into a string and trim() it before saving it to disc. – GordonM Aug 10 '15 at 13:42

2 Answers2

10

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

ficuscr
  • 6,975
  • 2
  • 32
  • 52
0

Your issue is the placement of Header('Content-type: text/xml');

Try this and make sure NOTHING comes before the header() call:

<?php 
header('Content-type: text/xml');

...

?>

I guess this could help you!

Samane
  • 500
  • 7
  • 23