The journal of David Brooks, designer, developer and photographer.

Using PHP and jQuery to Update RSS Feeds

I have been presented with the task of working on something… I can’t go into detail about it, but it has provided a few offshoots that I think will be very useful for future projects. The first one is a PHP and JQuery RSS feed reader.

Originally I used Magpie RSS for the feed parser, it worked very well. Then I spoke with Chris Davis who told me about the simplexml command for PHP. The code is literally very easy to work out. To parse an RSS feed directly in PHP I only have to write something like:

 <?php
$rss =  simplexml_load_file('Url of the Feed Here'); 
$title =  $rss->channel->title;
echo $title;
echo "<ul>";
// Here we'll put a loop to include each item's title and description
  foreach ($rss->channel->item as $item) {
  echo "<li><a href='" . $item->link . "'>" . $item->title . "</a></li>";
  // echo "<p>" . $item->description . "</p>";
 }
 echo "</ul>";
?> 

The above code is a modified version of Elliotte Rusty Harold’s code from an IBM article

What this code does, in a very simplified form, is open the feed, cut out the title and drop the contents of the feed into an unordered list. You could modify this out accordingly as needed if you wanted a different way to display your items.

The beauty of this method is that it will also handle authenticated RSS feeds, such as email feeds from Zimbra or Gmail. You just have to know the appropriate URL and specify a username and password like this:

username:password@URLGoesHere.com

Dropping that into the top of the code will give you a connection to a secured feed without much hassle. Though, it would then be up to you to provide a way to keep that feed secured on your end.

Integration with JQuery

If you wanted a feed that updated constantly, maybe an E-mail feed or the feed from a Twitter fanatic, you could parse that block of code from a separate PHP page and then import it into the rest of your site with an include call.

include "/whatever/whatever.php"; 

Then, from within your jQuery declarations you would write something like this:

var refreshId = setInterval(function() {
       $('#rss-feed1').load('rssfeed1.php');
       }, 3000);

What that code does is pretty interesting. First, it sets the variable “refreshId” and it tells it what to do. In this case it’s going to look for the object with the ID “#rss-feed1” (in my case it was a div) and it’s going to load the file rssfeed1.php within that div. The final part, the place where it says “3000,” is the amount of time between updates. In this case it’s set for 3 seconds between updates.

I always like to provide a fall back option for those who may not have JavaScript running on their machines. To accommodate those users I actually wrote in the PHP include on the page itself and then also used jQuery to overwrite it (using the code above.) I thought this was worth a mention because you could just use the jQuery chunk to include the PHP file directly.

So, there you have it, a way to update your RSS feeds, secured or not, on the fly with PHP and jQuery.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.

Recent Articles

Latest Comments

Northward Compass

Colophon

© 2005 - 2010 David Brooks, all rights reserved.