What is RSS Feeds?
Really Simple Syndication or RSS is a XML based file used to broadcasting website content such as latest news, blog post and other kind of website updates. RSS feeds can be download and read by RSS reader application. There are many RSS reader around but the most popular is NewzCrawler, FeedDemon, and Feedreader.
What is MagpieRSS?
MagpieRSS or usually called Magpie is a PHP class that can be used to parse or read RSS and Atom feeds from PHP application. Its currently supports RSS 0.9, RSS 1.0, Atom 0.3 and other most popular RSS format.
MagpieRSS works by fetching the content of RSS by specified url and saves the result to a file (called cache). The PHP programs then read the RSS content from the cache.
How can I use MagpieRSS with PHP and display it at my website?
In this post, I’ll explain two example scripts to read RSS feeds using PHP and and MagpieRSS. The first example is to read the latest news from http://www.reuters.com/ and the second example is to read the weather feed from http://weather.msn.com/.
Example 1 – Read RSS news feeds from http://www.reuters.com:
The first example explains how we can read the news feed from ‘Reuters Oddly Enough: And Finally’. The feed url is as following:
http://feeds.reuters.com/reuters/video/andfinally/rss/m4v?format=xml
Here is the source code:
<?php
require_once 'magpierss/rss_fetch.inc';
$url = 'http://feeds.reuters.com/reuters/video/andfinally/rss/m4v?format=xml';
$rss_data = fetch_rss($url);
if($rss_data)
{
echo "Site: ", $rss_data->channel['title'], “<br>\n”;
foreach ($rss_data->items as $rss_item ) {
$rss_title = $rss_item['title'];
$item_url = $rss_item['feedburner']['origenclosurelink'];
$rss_pubDate = $rss_item['date_timestamp'];
$location = $rss_item['location'];
echo “<a href=$item_url>$rss_title</a> - ” . date(”m/d/Y h:i A”, $rss_pubDate) . ” - ” . $location . “</li><br>\n”;
}
}
else
{
echo magpie_error() . “<br>”;
}
?>
First, we need to specify the url of the feed by assign it to variable $url. Then, by using the fetch_url() from MagpieRSS library, We read the feed data and put the data in the $rss_data variable.
The $rss_data variable contains the feeds data and each item is stored at the $rss_data->items arrays. We can simply extract each item by using the following code:
foreach ($rss_data->items as $rss_item ) {
$rss_title = $rss_item['title'];
$item_url = $rss_item['feedburner']['origenclosurelink']; $rss_pubDate = $rss_item['date_timestamp'];
$location = $rss_item['location'];
echo “<a href=$item_url>$rss_title</a> - ” . date(”m/d/Y h:i A”, $rss_pubDate) . ” - ” . $location . “</li><br>\n”;
}
Output:

Example 2 – Read weather data from http://weather.msn.com:
The second example is to read the weather data from http://weather.msn.com. This example is also explains how we can extract only selected data from RSS feed such as remove the links and get the image from the feeds.
Here is the feeds urls:
London - http://weather.msn.com/RSS.aspx?wealocations=wc:UKXX0085&weadegreetype=C
New York - http://weather.msn.com/RSS.aspx?wealocations=wc:USNY0996&weadegreetype=C
Tokyo - http://weather.msn.com/RSS.aspx?wealocations=wc:JAXX0085&weadegreetype=C
Hanoi - http://weather.msn.com/RSS.aspx?wealocations=wc:VMXX0006&weadegreetype=C
You can view the source code here.
This example is simply extend the function of the first example by eliminate the selected feed data. There are 3 additional functions used for this:
RemoveLinks() and DisabledLinks() – Remove every <a></a> tags from the data
RemoveMSN() – Remove unused information such as ‘More information at MSN Weather..’ and ‘Powered by Foreca’ data from the feeds. So we get only the weather data.
Output:

For both example, I’ve prepare the full source code. You can download it here:
Download Example 1
Download Example 2


















5 Comments Received
September 4th, 2008 @11:09 am
For me, iGoogle works fine as the default RSS reader..there are lots of other options but for some reason I set my igoogle page as my homepage and search and read from it.
October 18th, 2008 @10:55 pm
i love to have rss reader on my google desktop sidebar
computer problems′s last blog post..How to crack file password using google
November 1st, 2008 @5:59 pm
I’d love to incorporate this into my weather lens that I created. Thank you for explaining this.
SW′s last blog post..Weather Related Science Projects for Kids (and their parents
November 3rd, 2008 @3:17 am
Perhaps you can show us within a new post how to do that with a Feedburner feed? Having big problem with showing “feedburner:origLink”.
Wulffy′s last blog post..Social Bookmarking SEO - Das verrät niemand - bis jetzt
Pingback & Trackback
Leave A Reply