Line breaks in a PHP xmlwriter document

I have an XML feed that I created using XMLWriter . It works flawlessly in dev on the PHP 5.6 firewall. On a real server running PHP 5.4, the feed cannot display 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 are viewing the source, it looks like this:

source

Somehow, a couple of lines are added to the XML document. The only difference between the servers is the version of PHP (as far as I know).

Here are the first few lines of XMLWriter code:

 $xml = new XMLWriter(); $xml->openURI('php://output'); $xml->startDocument("1.0"); $xml->setIndent(true); $xml->startElement("propertyList"); $xml->writeAttribute('date', date('YmdH:i:s')); 

Any ideas how to get around this?

+8
source share
2 answers

Quite a lot of changes from PHP 5.4 to 5.6 ... not to mention changes to libxml ...

First of all, make sure that before opening the <?php tag or after the closing tag, if not, there is no space.

This will help if you can determine when new lines will be introduced (suppose these are new lines ... did you use something like a hex lookup?). Try writing to a temporary location — you want to determine if this happens when the page is being served or when xmlWriter is being output.

Things that come to mind ...

  • Perhaps it will be explicit what indetString should be. $xml->setIndentString(" ");

  • The default encoding ...? Maybe try and get this kit. I would expect when opening the xml tag ... encoding="UTF-8" . Use startDocument('1.0', 'utf-8'); and you should probably send a header, for example: header('Content-Type: application/xml; charset=UTF-8'); . Is your default_charset UTF-8?

  • What are the differences between the two environments? Things like short_open_tag etc.

    • LIBXML_HTML_NOIMPLIED ? Changed about 5.4?

Workaround:

  • Try calling ob_clean before starting writing to the output stream.

  • Use trim .

  • Upgrade the server that wants to be at 5.4 these days :)

+11
source share

Your problem is placing the header ('Content-type: text / xml');

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

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

I think this could help you!

+1
source share