MUniversity of Minnesota

January 07, 2005

How do I display RSS feeds on my blog?

A couple of entries ago I wrote about RSS feeds and how to make use of the RSS feeds on UThink. Now I'd like to turn our attention to RSS feeds outside of UThink and how to make use of them on your own blog. Many people have asked me if it is possible to display RSS feeds from other blogs outside of our system on their own blog home page(s). In the past, the answer has been no due to a limitation of how we originally set up the UThink server. Now however, thanks to the server upgrades alluded to in a previous post, it is possible to present RSS (or Atom) feeds from any blog or news service that provides them. Allow me to explain how.

When we upgraded the UThink server we cleaned up our installation of PHP and made it more functional and secure. What is PHP? In a nutshell, PHP is a programming language that can help you make your blog more dynamic. Using PHP and the "lastRSS.php" script I spoke about on December 10 you can put RSS based content onto your blog home page, or another page on your blog, that updates every time the RSS feed has new content to display. Sound cool? Let's give it a try then.

Since this is somewhat of an advanced skill, it is strongly recommended that you create a test blog to try this out on. If you are new to programming, or new to PHP, do not try this on your main blog until you are confident that you know what you are doing.

The first step you will need to perform is to change the name of your blog home page from "index.html" to "index.phtml." Why? Because by naming your "index" file with a ".phtml" extension you are telling the server that there will be some PHP on this file that the server will need to parse through. So:

  1. Log into UThink and select the blog you would like to test this out on.
  2. In the left hand menu of the administrative interface, click on Templates.
  3. Click on the "Main Index" template.
  4. In the "Output File" box, enter the text "index.phtml."
  5. Click on "Save".

What this will do is change your home page's main file into "index.phtml" rather than "index.html". Have no fear! This will not change the URL of your blog. Your URL will still be http://blog.lib.umn.edu/internetid/blogname/ .

Now we will enter the necessary PHP. At the top of the Main Index file, on top of all the other code, enter this snippet:



<?php

// include lastRSS
include "lastRSS.php";

// Create lastRSS object
$rss = new lastRSS;

// Set cache dir and cache time limit (1200 seconds)
// This will dictate how long the program should wait before it attempts to
// retrieve the RSS feed again.
$rss->cache_dir = '.';
$rss->cache_time = 1200;

?>



As you can see, this snippet of code makes use of the "lastRSS.php" program that will do the heavy lifting of going out and retrieving the content of the RSS feed you specify. Now, all you need to do is pick the feed you'd like to show and pick a place on your blog home page to show that feed. For the sake of this demonstration, I am going to use the "Top Stories" RSS feed from Yahoo! News. That feed can be found at http://rss.news.yahoo.com/rss/topstories. Now where should we put it. I would recommend somewhere in your sidebar underneath your calendar and links for your recent entries. Of course, you can pick where ever you want. If you pick the side bar, create a section for the feed and then enter this snippet of code:


<?php
// Try to load and parse RSS file
if ($rs = $rss->get('http://rss.news.yahoo.com/rss/topstories')) {
// Show website logo (if presented)
if ($rs[image_url] != '') {
echo "<a href=\"$rs[image_link]\"><img src=\"$rs[image_url]\" alt=\"$rs[image_title]\" vspace=\"1\" border=\"0\" /></a><br />\n";
}
// Show clickable website title
echo "<center><a href=\"$rs[link]\">$rs[title]</a></center><br />\n";
// Show website description
echo "$rs[description]<br />\n";
// Show last published articles (title, link, description)

echo "<ul>\n";
$num = 1;
foreach($rs['items'] as $item) {
if ($num <= 10) {
echo "\t<li><a href=\"$item[link]\">".$item['title']."</a><br />".$item['description']."</li>\n";
$num++;
}
}
echo "</ul>\n";
}
else {
echo "Error: It's not possible to reach RSS file...\n";
}

?>



Note the line "($rs = $rss->get('http://rss.news.yahoo.com/rss/topstories'))." Hopefully you have figured out that this is where you can put your own RSS feeds, or other feeds you or your readers may be interested in. Once you have entered the code snippet above, click "Save" within the administration interface of MT, and then "Rebuild." When it is finished, look at your blog home page to make sure everything looks good. If you'd like to see an example of what this shold look like, check out my test blog snackeru playground and look at the sidebar to the right. If you see a blank screen after you try this out it means that you have done something wrong with the PHP code. Go back, look for your errors, and try it again. Welcome to the world of programming!

Check out the lastRSS web site for more ways to render RSS feeds through PHP. If you have any questions, feel free to leave a comment!

Posted by snackeru at January 7, 2005 01:29 PM