Creating Your Own RSS Feed

October 12, 2009

There are many reasons to create an RSS feed – ranging from simply being a good way for people to keep up to date with what’s going on with your website, to a great interface for other applications such as Twitter.  If you have any sort of regularly updated content (game scores, news updates, blog, etc) then you should definitely consider creating an RSS feed.

I did just that for the Global Affiliate Network last night – we already had a MySQL driven News page, so this script pulls the last 10 articles and creates an RSS feed.  In my case, this was to publish on Twitter – but that’s a whole other article…


//Script to create RSS feed from our GAN news database
//Primarily for us to use in Twitter...

//Get our standard includes:
include "../includes/constants.php";

//Setup the opening info for the file:
$feed ="\r\n"
."\r\n"
."\r\n"
."\r\n"
."http://liveleads.org.uk/feed\r\n"
."This RSS feed keeps you up to date with what's going on at Global Affiliate Network.\r\n";

//Now open up our database, pull all the news articles (or the last 10) and create the body of the RSS
$mydb = mysql_connect($host, $user, $pass);

if (!mysql_select_db($db,$mydb)) { die("could not select database"); }

$sql = "select * from GAN_news order by news_date DESC limit 10;";
if (!$result = mysql_query($sql,$mydb)) die ("Could not retrieve news articles");

while ($article = mysql_fetch_array($result)) {
$feed .=''
.''
.'http://liveleads.org.uk/index.php?'
.'mode=news&article='.$article['news_id']
.''
.''.utf8_encode(html_entity_decode(stripslashes($article['news_content']))).''
.''.$article['news_date'].''
.'';
} // End while statement

//Close out the RSS feed:
$feed .= ''
.'';

header("Content-Type: rss+xml; charset=UTF-8");
echo $feed;

?>

This is a pretty tight piece of code – the only thing I will be working on is the encoding for the news_content – it took a lot of work to get this to display properly and pull into Twitter properly. I’m open to suggestions from any experts out there!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: