my github
english | espaƱol

RSS Feed For Your Needs

rss logo

By now you should have noticed that most of the times i like doing things myself, without any pre-made tools like WordPress, Blogger, Tumblr, etc. Not that i have anything against them. I just like to know how to do them, the best way is doing them myself.

Today i'm going to show you how i made this site's RSS feed. I had never done one before, and it is surprisingly easy to do. I'm not going to give you the introduction to what RSS is, because probably Wikipedia is the best way to go for it, so let's just say that for me, it is a nice standardized xml format which will let your "feed" to be read by content aggregators.

I will use PHP to build the XML, and MySQL to get the content. Just so you know, we'll make an XML similar to this:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:content="" >
<title> RSS feed</title>
<description>[Personal blog and sandbox of Waldo Urribarri]</description>
<copyright>Copyright (C) 2012-2013</copyright>
<title>Your post title</title>
<description>Your post content</description>
<link>Your post URL</link>
<pubDate>Your post publication date</pubDate>

The complete PHP for the feed is:

header("Content-Type: application/rss+xml; charset=UTF-8");

include 'dbconnection.php';

if(isset($_GET['lan'])) {
// Do something with the language
} else { // Use defaults }

if(isset($_GET['t'])) {
// build $query variable
} else {
// build $query variable

$myfeed = '<?xml version="1.0" encoding="UTF-8"?>';
$myfeed .= '<rss version="2.0">';
$myfeed .= '<channel>';
$myfeed .= '<title> RSS feed'.$extratitle.'</title>';
$myfeed .= '<link></link>';
$myfeed .= '<description>[Personal blog and sandbox of Waldo Urribarri]</description>';
$myfeed .= '<language>en-us</language>';
$myfeed .= '<copyright>Copyright (C) 2012-2013</copyright>';

$result = mysql_query($query, $conn) or die(mysql_error());

while($row = mysql_fetch_array($result)) {
$myfeed .= '<item>';
$myfeed .= '<title>'.$post_title.'</title>';
$myfeed .= '<description>'.$post_info.'</description>';
$myfeed .= '<link>'.$post_id.'</link>';
$myfeed .= '<pubDate>'.$post_date.'</pubDate>';
$myfeed .= '</item>';
$myfeed .= '</channel>';
$myfeed .= '</rss>';
echo $myfeed;

You really need a short amount of code as you can see. The first thing we do is set the content type to application/rss+xml and set the charset. Then i include my connection strings, do something with the data that i get from the URL and set my $query that will get the posts from the database. Nothing fancy there.

Then i start building my XML concatenating the first tags to the $myfeed variable. Of course, you should change this information according to your site. After the COPYRIGHT tag comes the most important part. For each result i start building a new ITEM, setting the TITLE, DESCRIPTION, LINK and PUBDATE information. After all posts are fetched from the database, i then close the CHANNEL and RSS tags and echo the resulting XML.

One thing to consider is that the DESCRIPTION tag can only contain text (this is not completly true but there are a lot of html tags that won't work). If you need to output html as i do, use this one instead:

<content:encoded><![CDATA[Your html content here]]></content:encoded>

That's pretty much it. Upload the .php file and test it yourself. To test it, you can subscribe to it using Google Reader or Feedburner.

Some browsers can tell when a site has an RSS feed, and that's because of a link element on the page. For this you can add this to the site that will be linked to your feed:

<link rel="alternate" href="/rssfeed" title="RSS feed" type="application/rss+xml" />

That's assuming your feed is located under root/rssfeed location, on a index.php.

Now what? Well, now that you have an RSS feed you could make a fancy iPhone/Android/Desktop app to pull the content from it and show it however you want. Just an idea.