Including an RSS Feed on your site…
Sep 26th, 2007 by Steven Lloyd Watkin
I’ve used my RSS feed generator on a few sites now, so I figure it’s about time I share. The final output validates and conforms to RSS2.0 standards so it will import into most things. I’ve even got mine linked to my facebook profile. The full code is at the bottom of the page.
Basically I use my RSS script to generate news feeds for the sites that I write. Examples can be seen on these sites,
But generally you can use the feed generator to serve any information you wish. For example would be the results of a search query where the query variables can be picked up from $_GET[]’s $_POST[]’s.
Anyway a question a friend asked me was “What’s the Point in RSS” good question! Click on the link to find out 
Right anyway onto the script. Firstly we need to tell the browser or news-aggregator that it’s looking at a rss:xml feed,
-
// Write the RSS header
-
header("Content-Type: text/xml;charset=utf-8"
;
It’s at this point that I usually set up my database connection and link to the information I require so usually it’ll be something like,
-
include_once( "./connect.php" );
-
$sql = mysql_query("select * from news order by ‘datePosted’ desc LIMIT 50;" );
I limit to 50 as no one wants to see 1000’s of posts going back years and years unless they specifically ask for it
Then we start writing out the ‘introduction’ part of the feed,
-
echo "<!–l version=\"1.0\" encoding=\"utf-8\"–>";
-
echo "<rss version="\"></rss>";
-
echo "<channel></channel>";
-
echo "";
-
echo "<description>YOUR SITE/FEED DESCRIPTION</description>";
-
echo "<link>".$_SERVER[’HTTP_SERVER’]."</link>";
Then we loop through our items (in this case news articles) printing the data out to our xml file,
-
// loop through the array pulling database fields for each item
-
while ($newsItem = mysql_fetch_assoc($sql))
-
{
-
$title = textFormat($newsItem[’news_title’]);
-
$link = $_SERVER[’HTTP_SERVER’]."news.php#news".$newsItem[’id’];
-
$description = textFormat($newsItem[’news_item’]);
-
$uid = "tag:".$_SERVER[’HTTP_SERVER’].",2007-09-26:rss.".$newsItem[’id’];
-
$pubDate = date("D, d M Y H:i:s O", strtotime($newsItem[’date_posted’]));
-
echo "<item></item>";
-
echo "";
-
echo "<link>".$link."</link>";
-
echo "<description>".$description."</description>";
-
echo "
-
<pubdate>".$pubDate."</pubdate>";
-
echo "<guid isPermaLink="\">".$uid."</guid>";
-
echo "";
-
}
Remember for our RSS feed all text should be UTF-8 encoded so we include a little function to do this,
-
function textFormat($intext) { return utf8_encode(htmlspecialchars(stripslashes($intext)));}
Then it’s just a simple case of closing down our xml tree and we’re all done,
-
echo "";
Pretty painless wasn’t it
And you can use these feeds to output anything of use. If someone is returning to your site to check for updated information should it be course dates, news items, forum posts, blog entries, even property searches it can all be done using RSS feeds.
-
<!—->//==========================================
-
// Author: Steven Lloyd Watkin
-
// Company: Evilprofessor Designs
-
// Website: http://www.ep-projects.co.uk
-
// Date: June 2007
-
// Feel free to use and modify, but please
-
// include reference to my site somewhere
-
//==========================================
-
-
function textFormat($intext) { return utf8_encode(htmlspecialchars(stripslashes($intext)));}
-
-
header("Content-Type: text/xml;charset=utf-8"
; -
echo "<!–l version=\"1.0\" encoding=\"utf-8\"–>";
-
echo "<rss version="\"></rss>";
-
echo "<channel></channel>";
-
-
// retrieve database records
-
include(’connect.php’
; -
$sql = mysql_query("select * from ‘news’ order by ‘datePosted’ desc"
; -
-
echo "";
-
echo "<description>YOUR SITE/FEED DESCRIPTION</description>";
-
echo "<link>".$_SERVER[’HTTP_SERVER’]."</link>";
-
-
// loop through the array pulling database fields for each item
-
while ($newsItem = mysql_fetch_assoc($sql))
-
{
-
$title = textFormat($newsItem[’news_title’]);
-
$link = $_SERVER[’HTTP_SERVER’]."news.php#news".$newsItem[’id’];
-
$description = textFormat($newsItem[’news_item’]);
-
$uid = "tag:".$_SERVER[’HTTP_SERVER’].",2007-09-26:rss.".$newsItem[’id’];
-
$pubDate = date("D, d M Y H:i:s O", strtotime($newsItem[’date_posted’]));
-
echo "<item></item>";
-
echo "";
-
echo "<link>".$link."</link>";
-
echo "<description>".$description."</description>";
-
echo "<pubdate>".$pubDate."</pubdate>";
-
echo "<guid isPermaLink="\">".$uid."</guid>";
-
echo "";
-
}
-
echo "";
-
?>





