RSS feeds

Today I have been looking into ways of processing and outputting RSS feeds. The best way of processing feeds is with a free PHP extension called Magpie RSS this will then allow you to call the function/program from within your own script and it will allow you to access all the various metadata within the RSS/XML feed.

The benefit of using Magpie RSS is that it will automatically cache the RSS feeds and will only update the feed when it there are updates. This greatly improves the overall speed of loading up a page with allot of RSS feeds on it, as the system will only need to request the feed online once and all subsequent requests for that feed will use a cached version stored in its cache folder
For example to load the BBC News RSS feed, the following command can be used:

PHP:
  1. require_once('MapieRSS/rss_fetch.inc');
  2. $url = 'http://news.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss091.xml';
  3. $rss = fetch_rss( $url );

This will then make a request and then cache that request. Each news item can be accessed by calling its specific tag, so to pull out the title of the article I can use $item['title'] and the same can be done to get a short description of that article $item['description']. So to display the complete feed the follow command can be used:

PHP:
  1. echo "Channel Title: " . $rss->channel['title'] . "";
  2. <ul>";
  3. foreach ($rss->items as $item) {
  4. $href = $item['link'];
  5. $title = $item['title'];
  6. $description = $item['description'];
  7.     <li><a href="http://blog.duomesh.com/$href">$title</a>
  8. $description</li>
  9. ";
  10. }
  11. echo "</ul>
  12. ";

Comments are closed.