Header for XML content in PHP file

If you output XML content from PHP file, you need to set content-type header, so that your browser know that you are going to output XML content from PHP file instead of default content-type header (text/html).
File: php_file.php
<?php
header ("Content-Type:text/xml");
?>
<your_xml_content>
</your_xml_content>
You may know, no output (no html, echo, space etc) can occur before "header()" for header being sent to browser.

Content-type header tells browser how to handle the content. For example, if there is media file then browser will give you an option to use another application if it cannot handle that content. For *.mp3 file, it may ask you to use any applications installed on your computer that is associated with mp3 or it will tell you to download the content.
text/xml file is treated as us-ascii file if there is no charset specified. So, if your charset is different then specify that in the header as below.

Update:

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

application/xml with charset can be good, if you are preparing for any application to consume the xml file. With charset specified, there should not be any problem with any of the format.

  • # 1 - by Danish

    Satya Prakash Karan
    thanks ver much

    i found very easy after getting this from you

    admin@yiippee.com

  • # 2 - by Danish

    Satya Prakash Karanthanks ver muchi found very easy after getting this from youadmin@yiippee.com

  • # 3 - by John Minton

    Just a quick note if anyone is having some trouble, I read something yesterday stating that a space is needed between the colon and the word “text”, like so:

    This rule should apply to any header as far as I’m aware. ALWAYS put a space after the colon. I guess some browsers have issues with that or something? Maybe it’s because the RFC defines the Content-Type as such?…just my 2 cents.

    I am working on http://theghostbay.org, creating a site map. Since my most important content is in a database, I can can create a list of all the links on my site really easily. All that is needed is to make it an XML file, using the content header to achieve this from a php file.

    Code Sample:

    <?php
    // connect to database
    include("config.php");

    // set header
    header("Content-Type: text/xml");

    // start xml content for site map
    echo "
    “;

    // query
    $gt = mysql_query(“SELECT * FROM pages”);

    // loop through query
    while($agt = mysql_fetch_array($gt)){

    // Extract…if a column name is “col”, then extract will turn it into “$col”.
    // It pulls it out of the array!
    // so $agt['col'] has now been converted to $col.
    // This is an awesome function, http://php.net/extract
    // Cols: id name torrent description
    extract($agt);

    // ,, and are the only required entries as such:
    echo ”

    http://theghostbay.org/index.php?torrent=$id
    $lastmodified
    monthly
    0.6
    “;
    }

    echo “”;

    // read more about site maps via http://www.sitemaps.org/protocol.php

    ?>

  • # 4 - by John Minton

    Just a quick note if anyone is having some trouble, I read something yesterday stating that a space is needed between the colon and the word “text”, like so:

    This rule should apply to any header as far as I’m aware. ALWAYS put a space after the colon. I guess some browsers have issues with that or something? Maybe it’s because the RFC defines the Content-Type as such?…just my 2 cents.

    I am working on http://theghostbay.org, creating a site map. Since my most important content is in a database, I can can create a list of all the links on my site really easily. All that is needed is to make it an XML file, using the content header to achieve this from a php file.

    Code Sample:

    <?php
    // connect to database
    include("config.php");

    // set header
    header("Content-Type: text/xml");

    // start xml content for site map
    echo "
    “;

    // query
    $gt = mysql_query(“SELECT * FROM pages”);

    // loop through query
    while($agt = mysql_fetch_array($gt)){

    // Extract…if a column name is “col”, then extract will turn it into “$col”.
    // It pulls it out of the array!
    // so $agt['col'] has now been converted to $col.
    // This is an awesome function, http://php.net/extract
    // Cols: id name torrent description
    extract($agt);

    // ,, and are the only required entries as such:
    echo ”

    http://theghostbay.org/index.php?torrent=$id
    $lastmodified
    monthly
    0.6
    “;
    }

    echo “”;

    // read more about site maps via http://www.sitemaps.org/protocol.php

    ?>

Comments are open for an year period. Please, write here on Facebook page.