0

Is there something else to use instead of header("Content-type: text/xml"); I would like to generate XML out of my database and I keep getting the following error:

Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/php32/Eindopdracht/api.php:1) in /Applications/MAMP/htdocs/php32/Eindopdracht/core/functions/api.php on line 41

I already checked of I had any white spaces in it but I don't think that is causing the error. This is the function I wrote in PHP:

function generateXML(){
    $sql="SELECT * FROM csvupload";
    $result=mysql_query($sql);
    $xml = new XMLWriter();

    $xml->openURI("php://output");
    $xml->startDocument();
    $xml->setIndent(true);

    $xml->startElement('chords');

    while ($row = mysql_fetch_assoc($result)) {
      $xml->startElement("chord");

      $xml->writeAttribute('E', $row['item1']);
      $xml->writeAttribute('a', $row['item2']);
      $xml->writeAttribute('d', $row['item3']);
      $xml->writeAttribute('g', $row['item4']);

      $xml->endElement();

    }

    $xml->endElement();
    header('Content-type: text/xml');
    $xml->flush();
}
Mae
  • 443
  • 2
  • 5
  • 22

2 Answers2

2

Your header calls have to happen before anything is output to the browser.

Chances are, the first line of your file is something like [space]<?php - removing the space should do the trick.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
  • I've already checked if there were any unnecessary spaces but I couldn't find any. – Mae Jan 14 '15 at 16:35
  • Paste us the first couple lines of your file, then. This error is **always** caused by the same set of things, and whenever we see people go "nope, I checked that" it turns out that they missed something. – ceejayoz Jan 14 '15 at 16:36
  • This is the whole PHP file https://eval.in/242405 - P.S. I know that usuing mysql instead of mysqli is bad but I'm only using it for learning purposes. – Mae Jan 14 '15 at 16:38
  • @Mae Your error says your header call is on line 41, but your code shows that call on line 40. Check that you don't have a blank line at the top of the file. – ceejayoz Jan 14 '15 at 16:40
  • 1
    I might be wrong on this but hasn't @Mae opened an output pipe at : `$xml->openURI("php://output");` ... so the `header` has to be before that. – CD001 Jan 14 '15 at 17:05
  • @CD001 Possibly, I'm not sure. Worth moving the line up to the top of the function, certainly. – ceejayoz Jan 14 '15 at 17:20
  • CD001 solved it! I had to put the header at the beginning of the function. I would like to thank both of you :) I'm still learning PHP and I used mysql instead of mysqli for learning purposes! Anyway you guys are amazing! Thanks! – Mae Jan 14 '15 at 20:21
0

what is your text editor? i had this problem before, i use Notepad++ in Encoding menu there is an option named Encode in UTF-8 without BOM, you should select it or something like that in other editors.

you can make sure by save an empty file and check it's file size.

you can find out more about BOM here http://en.wikipedia.org/wiki/Byte_order_mark

Kiyan
  • 2,155
  • 15
  • 15
  • Hi Kiyan, thanks for you reply. I heard of it before but I already tried doing this. I use Sublime Text to edit code. :) – Mae Jan 14 '15 at 20:19