I'm trying to generate a valid RSS file from a MySQL database. Everything looks ok and is readable by a newsreading program, but when I try to validate the feed, I get a bunch of errors.
Part of the problem is that there are blank lines at the beginning of the file, that I can't seem to get rid of and I don't know where they are coming from.
It is also telling me that "feeds should not be served with the text/html media type" even though I'm using the header() function to specify the content type as XML.
I tried using the ob_ functions but I'm clearly missing something.
Code is below. Can anyone help me figure this out? If you want to see a copy of the XML that is generated, it is here:
http://www.inewsclips.com/Newsclips/php/rss.php
<?php
require("./includes/newsclips_fns.php");
set_prefs();
db_connect();
$today = getdate();
$seconds= ((($daystodisplay24)60)60);
$daysbefore= getdate(time()-$seconds);
$limit=100;
$sql=("SELECT ,DATE_FORMAT(Article_Date, '%m/%d/%Y') as Date FROM articles WHERE Article_Date >='".$daysbefore["year"]."-".$daysbefore["mon"]."-".$daysbefore["mday"]."' ORDER BY Article_Date DESC, Source LIMIT ".$limit);
$result = mysql_query($sql);
ob_clean();
header("Content-Type: application/xml",true);
ob_start;
$output .="<?xml version=\"1.0\"\x3F><!DOCTYPE rss PUBLIC \"-//Netscape Communications//DTD RSS 0.91//EN\" \"http://my.netscape.com/publish/formats/rss-0.91.dtd\"><rss version=\"2.0\">
<channel>
<title>".$sitename."</title>
<link>[url]http://[/url]".$_SERVER['SERVER_NAME']."</link>
<description>Top ".$limit." ".$item_label."s for ".$institution_name."</description>
<language>en</language>\n";
$counter=1;
$rownum=0;
while ($myrow = mysql_fetch_array($result)) {
$output .= "<item>\n<title>[".$myrow['Category']."] ".htmlentities($myrow['Title_of_Article'])." by ".$myrow['Author_of_Article'].", ".$myrow['Source']."</title>\n";
$formattedtext = $myrow['Text_of_Article'];
$formattedtext = str_replace("\n", "", $formattedtext); // to store the string
$formattedtext=htmlentities($formattedtext);
$pattern = "/(.{0,500})(\W+.*$)/";
$replacement = "\${1}";
$formattedtext=preg_replace($pattern, $replacement, $formattedtext); // Returns "Don't"
$output .= "<description>".$formattedtext."</description>\n<pubDate>".$myrow['Date']."</pubDate>\n<link>[url]http://[/url]".$_SERVER['SERVER_NAME']."/Newsclips/php/article_detail.php?ArticleID=".$myrow['ArticleID']."&RowNumber=".$rownum."</link>\n</item>\n";
$counter=$counter+1;
$rownum=$rownum+1;
}
$output .= "</channel></rss>";
$output = ltrim($output);
echo $output;
ob_end_flush();
?>