PHP: Display an RSS/Twitter feed on your site with RssRepeater
by Gritfish on July 20, 2011
Rss is great – it allows your sites content to be loaded and shared by just about anything. It also lets you pull together content from many sources and display them in a single page.
Last year I built http://www.blaggregator.com as a way of teaching myself all the ins and outs of html, php and css. A big part of the functionality of Blaggregator (aside from merging and re-sharing rss feeds) is taking rss feeds and displaying them as html. This can be done with twitter feeds, blogs, and anything else that outputs rss. To aid in showing rss feeds on my own websites, I isolated the display code into a single php class.
Download the php file here
Using the RssRepeater class:
The RssRepeater class needs just 3 things:
- The url of the rss feed you want to display
- How you want the html to appear
- How many items you want to show
In this example code, we’re going to pull the 5 most popular shots on http://www.dribbble.com
<?php
include 'RssRepeater.php';
$template = '<div>
<h3>[TITLE]</h3>
[IMAGE]
<p>[DESCRIPTION]</p>
<a href="[LINK]" target="_blank">[LINK]</a>
</div>';
$rss = new RssRepeater('http://dribbble.com/shots/popular.rss', $template, 5);
?>
<div>
<h1><a href="<?php echo $rss->link; ?>"><?php echo $rss->title; ?></a></h1>
<p><?php echo $rss->description; ?></p>
<?php echo $rss; ?>
</div>
What this code does:
The $template variable is just a chunk of html code with placeholders that RssRepeater will replace with information from the feed. You can use all the following tags in your html:
- [TITLE]
- [DESCRIPTION]
- [LINK]
- [IMAGE]
- [IMAGE_URL]
Properties of the actual feed itself (not an item IN the feed) are accessible with:
- $rss->title – The name of the feed
- $rss->link – a link to the site that contains the feed, not a link to the feed itself
- $rss->description – a description of the feed