Header for XML content in PHP file
Feb 27th, 2008
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. If there is media file then it will give you option to use another application if it cannot handle that content. For *.mp3 will it ask you to use any applications installed on your computer and that is associated with mp3 or it will tell you to download the content.
Content-type header tells browser how to handle the content. If there is media file then it will give you option to use another application if it cannot handle that content. For *.mp3 will it ask you to use any applications installed on your computer and that is associated with mp3 or it will tell you to download the content.

Satya Prakash Karan
thanks ver much
i found very easy after getting this from you
admin@yiippee.com
Satya Prakash Karanthanks ver muchi found very easy after getting this from youadmin@yiippee.com
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
?>
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
?>