Including an RSS Feed on your site…

By , Wednesday 26th September 2007 3:31 pm

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.com – Social Networking”>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 ""; 
echo ""; 
echo ""; 
echo ""; 
echo "YOUR SITE/FEED DESCRIPTION"; 
echo "".$_SERVER['HTTP_SERVER']."";

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 ""; 
    echo ""; 
    echo "".$link.""; 
    echo "".$description.""; 
    echo "
".$pubDate.""; 
    echo "".$uid.""; 
    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.

Here’s the entire script:

//========================================== 
// 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 ""; 
echo ""; 
echo "";  

// retrieve database records 
include('connect.php'); 
$sql = mysql_query("select * from 'news' order by 'datePosted' desc");  

echo ""; 
echo "YOUR SITE/FEED DESCRIPTION"; 
echo "".$_SERVER['HTTP_SERVER'].""; 

// 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 ""; 
    echo ""; 
    echo "".$link.""; 
    echo "".$description.""; 
    echo "".$pubDate.""; 
    echo "".$uid.""; 
    echo ""; 
} 
echo ""; 
?>

Liked this post? Follow this blog to get more. 

Leave a Reply

You must be logged in to post a comment.

Panorama Theme by Themocracy

2 visitors online now
1 guests, 1 bots, 0 members
Max visitors today: 6 at 04:56 am UTC
This month: 33 at 18-03-2017 08:20 pm UTC
This year: 45 at 02-01-2017 10:28 pm UTC
All time: 130 at 28-03-2011 10:40 pm UTC