change mime type of output in php

PhpXmlMime Types

Php Problem Overview


I've got a php script. Most of the time the script returns html, which is working fine, but on one occasion (parameter ?Format=XML) the script returns XML instead of HTML.

Is there any way to change the returned mime type of the php output on the fly from text/html to text/xml or application/xml?

Php Solutions


Solution 1 - Php

header('Content-type: application/xml');

More information available at the PHP documentation for header()

Solution 2 - Php

Set the Content-Type header:

header('Content-Type: text/xml');

Though you should probably use "application/xml" instead.

Solution 3 - Php

You should send a Content-Type header before you send any output.

header('Content-Type: text/xml');

Solution 4 - Php

I will answer to the update, since the previous answers are good.
I have read that Internet Explorer is well known for ignoring Mime type headers (most of the time?) to rely on content of the file (which can cause problems in some cases).

Mmm, I did a simple test:

<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root><foo a="b">Tada</foo></root>';
?>

Internet Explorer 6 displays it correctly as XML. Even if I remove the xml declaration.
You should indicate which version is problematic.

Actually, as I wrote above, with IE (6 at least), you don't even need a content-type, it recognize XML data and display it as a tree. Is your XML correct?

[Update] Tried with IE7 as well, adding ?format=xml too, still displaying XML correctly. If I send malformed XML, IE displays an error. Tested on WinXP Pro SP2+

Solution 5 - Php

header('Content-Type: application/xml; charset=utf-8');

You can add encoding as well in the same line. I added utf-8, which is most common.

Solution 6 - Php

I just used the following:
NOTE: I am using "i" for sql improved extension.

Start XML file, echo parent node
header("Content-type: text/xml");
echo "<?xml version='1.0' encoding='UTF-8'?>";
echo "<marker>";

Iterate through the rows, printing XML nodes for each

while ($row = @mysqli_fetch_assoc($results)){
  // Add to XML document node
  echo '<marker ';
  echo 'id="' . $ind . '" ';
  echo 'name="' . parseToXML($row['name']) . '" ';
  echo 'address="' . parseToXML($row['address']) . '" ';
  echo 'lat="' . $row['lat'] . '" ';
  echo 'lng="' . $row['lng'] . '" ';
  echo 'type="' . $row['type'] . '" ';
  echo '/>';
}

// End XML file
echo "</marker>";

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSamView Question on Stackoverflow
Solution 1 - PhpnickfView Answer on Stackoverflow
Solution 2 - PhpJohn MillikinView Answer on Stackoverflow
Solution 3 - Phpandy.gurinView Answer on Stackoverflow
Solution 4 - PhpPhiLhoView Answer on Stackoverflow
Solution 5 - PhpUsman AhmedView Answer on Stackoverflow
Solution 6 - PhpThomasView Answer on Stackoverflow