58

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?

Sam
  • 26,538
  • 45
  • 157
  • 240
  • Can you provide more info about what you're trying to do? i.e. RSS, xhtml etc.? I also found this bug: http://support.microsoft.com/kb/264868/ – joelhardi Sep 30 '08 at 07:16
  • Make sure you are using the header call before any text is outputted – Eran Galperin Sep 30 '08 at 07:37
  • Whoah, my mistake, it works in IE - my XML generation code had a big big mistake (I copied it from some tutorial, and didn't really read the output line, which converted the XML into HTML using htmlentities. Sorry!!! – Sam Sep 30 '08 at 09:17
  • PS.: Since you are wondering, it seems that on SO, the usage is to select the first correct answer... :-) Which is logical. – PhiLho Sep 30 '08 at 09:29
  • Yeah, seems the best way - even though the other answers contain valid points the first answer is missing (send before any other output, use application/xml instead of text/xml). Thanks! – Sam Sep 30 '08 at 09:37

6 Answers6

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

More information available at the PHP documentation for header()

nickf
  • 499,078
  • 194
  • 614
  • 709
  • 1
    It happened to me that the Microsoft Visual Studio API generating web service client doesn't like the "application/xml", it requires "text/xml" instead. – GTodorov Jul 25 '17 at 14:59
19

Set the Content-Type header:

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

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

John Millikin
  • 183,900
  • 37
  • 203
  • 216
  • 1
    You got me - what is the difference between text/xml and application/xml, though? – Sam Sep 30 '08 at 07:11
  • Interactions between text/* content types HTTP regarding encoding, mainly: see http://annevankesteren.nl/2005/03/text-xml – John Millikin Sep 30 '08 at 07:33
  • 6
    Caching proxies are allowed to edit `text/*` to change the character encoding. Since XML also declares its character encoding inside the document itself, you really don't want that to happen, as you might get a mismatch. So use `application/xml` instead, and caching proxies will see the content as binary data and leave it undisturbed. – TRiG Jul 20 '10 at 17:11
4

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

header('Content-Type: text/xml');
andy.gurin
  • 3,526
  • 1
  • 19
  • 14
3

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+

PhiLho
  • 38,673
  • 6
  • 89
  • 128
  • Sorry, my mistake, the example code I copied did sent the XML through htmlentities for output, and I didn't notice until just now! – Sam Sep 30 '08 at 09:21
  • Ah! OK. :-) We all learn from errors (even those of other people!). – PhiLho Sep 30 '08 at 09:27
  • I think it's text/xhtml that IE 6 has a problem with; I've never played with it, but I've heard many complaints about it. I believe that IE 6 will simply not work if you send text/xhtml, but I'm not sure. – dirtside Sep 30 '08 at 14:34
  • One type IE has problems with is XHTML, which is application/xhtml+xml or application/xml -- IE does not render XHTML sent with either of these correct content-types (you can incorrectly send as text/html to force IE to use its HTML mode, but this is wrong and can cause other side effects). – joelhardi Sep 30 '08 at 18:13
3
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.

Usman Ahmed
  • 1,918
  • 1
  • 15
  • 15
1

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>";
ankit suthar
  • 2,624
  • 6
  • 29
  • 49
Thomas
  • 46
  • 4