[php/rss] Serving PHP files as RSS

Associate
Joined
2 Apr 2004
Posts
674
Location
Melbourne
I have a file on my website which is a php file reading from a MySQL database.

Address of feed is: http://www.caughtinthecrossfire.com/feeds/feed.rss

(I have mod_rewrite setup to redirect the address to the php file)

Now when I try to validate the feed, it says that I'm not serving the file as xml even though I have the following header...

Code:
header("Content-type: application/xml");

anyone know what I need to do in order to get php serving this as xml?
 
Last edited:
Code:
<?
header("Content-type: application/xml");
require("../db.php");
require("../functions.php");
echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?".">";
?>
<rss version="2.0">
	<channel>
		<title>Caught in the Crossfire - Articles</title>
		<link>http://www.caughtinthecrossfire.com/</link>
		<description>Caught in the Crossfire is an online UK magazine promoting Unadulterated Malarkey for drunken youth. We dedicate our time to bring you the best in Music, UK Skateboarding and anything else we can find to write about. Featuring interviews with the best bands, artists and pro skateboarders, all updated daily for your bored existence from London, UK</description>
		<generator>nav...</generator>
		<language>en</language>
<?
$query = "SELECT cf_posts.ID, cf_posts.post_title, cf_posts.post_excerpt, cf_posts.post_date_gmt, cf_categories.category_parent, cf_categories.category_dir, cf_categories.cat_name FROM `cf_posts` LEFT JOIN cf_post2cat ON cf_posts.ID = cf_post2cat.post_id LEFT JOIN cf_categories ON cf_categories.cat_ID = cf_post2cat.category_id WHERE cf_categories.cat_base = 2 ORDER BY `post_date_gmt` DESC LIMIT 0, 20";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)){ 
		$url = $row['category_dir']."/".$row['ID'];
		$category = $row['cat_name'];
		if (0 != $row['category_parent']){
			$innerquery = "SELECT cf_categories.category_dir FROM cf_categories WHERE cf_categories.cat_ID = ".$row['category_parent'];
			$innerresult = mysql_query($innerquery) or die();
			$dir = mysql_result($innerresult, 0, 'category_dir');
			$url = $dir."/".$url;
		}			
		$url = DOMAIN.$url;
?>
		<item>
			<title><![CDATA[<? echo make_xml_safe($row['post_title']); ?>]]></title>
			<link><? echo make_xml_safe($url); ?></link>
			<pubDate><? echo date('r', strtotime($row['post_date_gmt'])); ?></pubDate>
			<author>[email protected]</author>
			<category><? echo make_xml_safe($category); ?></category>
			<guid><? echo make_xml_safe($url); ?></guid>
			<description>
				<![CDATA[<? if($row['post_excerpt']){ echo make_xml_safe($row['post_excerpt']);} else { echo "No Details"; } ?>]]>
			</description>
		</item>	
<? } ?>
	</channel>
</rss>
 
There's no whitespace in any of the included files?

If you send so much as a newline before the header() call, the webserver will send it as text/html by default.
 
robmiller said:
There's no whitespace in any of the included files?

If you send so much as a newline before the header() call, the webserver will send it as text/html by default.
You'd get a PHP error if that had happened. :)

This should work. I've produced RSS feeds from PHP and they work fine. I use text/xml and ISO-8859-1 instead of application/xml and utf-8, but I don't see why that should make any odds. I did notice that your PHP script sends back

Code:
<?xml version="1.0" encoding="utf-8" ?><rss version="2.0">
You might need a newline in there. :)
 
Back
Top Bottom