Tues, 1 Jan 2008 19:48:00 EST

Using PHP to Parse and Display an RSS Feed

Just thought I'd share a little PHP script that I wrote recently which takes an RSS feed, parses it and echoes it out on a page. The code itself is very simple and based on PHP5's simplexml_load_file function. The first section of code takes a predefined RSS feed, $feed, and loads the feed's XML into the variable $xml via the simplexml_load_file function. I've also added an @ in front of the function to suppress the long list of warnings and errors produced by a feed that fails to load. I then check to see that something has, in fact, been loaded into $xml, then begin loading the feed data. Accessing an individual element within an XML document is just as easy as loading the document itself. To access an element in the document, you simply write out the path of the element with each level of the tree separated by "–>". For example, $site = $xml->channel[0]->title[0]; will load the title of the news feed into the variable $site. Check out the code and live demo below. This example takes the BBC World News front page RSS and echoes the channel information and the first 20 feed items.

I'm currently working on a Netvibes-style application that is based off of the use of this function. Check back in a few weeks when I'll be sharing a much more in depth version of the RSS reader.

View a Live Demo »
<?php

$feed = '<Insert Feed URL>';
$xml = @simplexml_load_file($feed);

if($xml) {
  $siteTitle = $xml->channel[0]->title[0];
  $siteLink = $xml->channel[0]->link[0];
  $siteDesc = $xml->channel[0]->description[0];
  echo "<strong>$siteTitle<br/>
        <a href=\"$siteLink\">
          $siteLink
        </a>
        <br/>$siteDesc</strong><br/><br/>";

  for($i=0; $i<20; $i++) {
    $itemTitle = $xml->channel[0]->item[$i]->title[0];
    $itemLink = $xml->channel[0]->item[$i]->link[0];
    $itemDesc = $xml->channel[0]->item[$i]->description[0];
    echo "$itemTitle<br/>
          <a href=\"$itemLink\">
            $itemLink
          </a>
          <br/>$itemDesc<br/><br/>";
  }
} else { 
  echo "ERROR: Failed to open stream"; 
}

?>
Bookmark and Share

6 Comments So Far

dhananjay
Sunday, 13 January 2008 13:14
this is very easy to undestand.
Jason
Friday, 18 January 2008 12:43
Excellent, just plugged in a beeb feed and away it went.
serkan temizl
Sunday, 20 January 2008 8:33
The easiest way to parse a RSS 
Matt
Monday, 21 January 2008 10:27
Awesome!!!  Thanks for this.  I had no idea this command existed in php 5.
Andrew Peel
Monday, 11 February 2008 7:57
A silent fail would suggest PHP4 installed on my server, correct?Any alternative PHP4 code for simple_load_file ?
Sussex Web Design
Tuesday, 28 October 2008 7:43
Just what we were looking for thank you. By the way how could you get it to only show the first X characters from the description?ThanksD

Post a comment


If you are seeing this message, you are either using a screen reader or you have disabled CSS. Please do not fill in the following form field, titled "lastname", or your comment will not be saved. This field is only used to help prevent spam comments.


Welcome

JustinSpegele.com is where I share projects that I'm working on, php tutorials, web development tips and tricks, and random thoughts.

#MLS has a very odd notion of an all-star game. MLS all-stars vs. an EPL team? Still, should be very fun to watch. MLS 3 - Man United 2 1:41 PM Jul 28th from web
The New Digg And The Future Of Social News http://bit.ly/cM7Guq 10:44 AM Jun 29th from TweetMeme
so as of right now, the Big 10 has 12 teams and the Big 12 has 10 teams? http://sports.espn.go.com/ncaa/news/story?id=5276668 7:59 PM Jun 11th from web
heading to Las Vegas for #caworld in the morning 7:12 PM May 12th from web
Found out from CNN that I went to high school with a convicted terrorist who worked with Al-Qaeda. Not what I expected to see on the news 9:49 PM May 10th from web
View all posts on Twitter »

Friend me

Twitter Digg Facebook FriendFeed Del.icio.us iFanboy Last.fm Squidoo

Login

Username:

Password: