Use PHP to display any RSS feed on your site

September 18, 2007 / Filed under: Code, PHP, RSS, Tutorials, Web Development, XML

Over the years, I've struggled with ways to include external content on this site, without having to use JavaScript or Flash "widgets," which are convenient but can be troublesome.

I even got philosophical and preached that external content should remain external - relative to it's domain.

Despite all this, occasionally I'll want to display some content from another site, such as my Twitter updates, or my Google Reader Shared links.

With PHP 5, it's very easy to include external content onto your site. This tutorial will explain how to display the contents of an RSS feed directly into your site's HTML markup.

The benefits of parsing external content are:

  1. You're not stuck with whatever "badge" or "widget" they provide you, which is often hard to customize to match your own site's design and layout.
  2. The content you display is accessible by search engines, since it actually becomes part of your site, helping to increase relevancy and page-rank. ("Widget content" is often invisible to search engines.)
  3. Page loading speed is not affected.

RSS feed

First, find the RSS feed URL for the content you'd like displayed on your site. For this example, we'll use my latest Twitter updates RSS feed:

http://twitter.com/statuses/user_timeline/122933.rss

Below is an example "node" of this RSS feed:

PHP

With PHP, we can parse this information, and display it in a format more comfortable to us, such as a list item:

  • Matthom: I've come to the conclusion there is no one-stop-shop browser.

Using basic cURL syntax, we'll loop through each <item> element from the RSS feed, and display it as an HTML unordered list:

Result

This code will loop through each RSS feed <item>, and output my last ten Twitter updates as an HTML unordered list:

  • Matthom: I've come to the conclusion there is no one-stop-shop browser.
  • Matthom: At the iTunes store.
  • Matthom: @MattsShell - We should go to Jewel and pick some up.
  • Matthom: Collective Soul - Georgia Girl
  • Matthom: woo chilly!
  • Matthom: Suppose to be 85 so am wearing shorts again
  • Matthom: heading out the door.
  • Matthom: I need to start buying some frozen lunch meals
  • Matthom: Is it only Tuesday?
  • Matthom: Going to eat. Tacos!

Nice, clean, and looks like it comes straight from my own database.

Other resources

Comments/Mentions

# David Hall at 9/20/2007 1:57 am cst

Hi, I'm trying your method within a TextPattern istallation - therefore between ~ and getting the following error:

Parse error: syntax error, unexpected '<' in /users/home/davidhall/web/public/textpattern/publish/taghandlers.php(2745) : eval()'d code on line 2

Test page is: http://david-hall.info/trial

Probably something I'm doing wrong, but any ideas?

Thanks, David

# Bonnie at 2/6/2008 8:08 pm cst

I'm not too familiar with PHP, just messed around with some real basic stuff. I added your code to my page, but am getting an error - call to undefined function. Is there some other code that needs to be added?

# Memzap at 5/23/2008 5:24 pm cst

It would be awsome if it worked.

# Jon Konrath at 7/24/2008 10:15 am cst

If you're having trouble getting this to work with your not-twitter RSS feed, adjust the line foreach ( $xmlObjTwitter -> item as $item )

I had to change it to foreach ( $xmlObjTwitter->channel->item as $item )

# Kyle Benson at 10/24/2008 12:40 pm cst

Thanks you Jon! That worked great.

# Jesper Hills at 11/6/2008 4:46 am cst

Very helpful, thanks!

# nick at 1/19/2009 1:06 pm cst

Hi Matt, Thanks for the snippet...works great except you should update the twitter rss structure to: foreach ( $xmlObjTwitter -> status as $item ). Best, Nick

# Jenny at 2/6/2009 8:45 pm cst

I was able to utilize this code, thank-you very much. I need to be able to make the feed I'm using span multiple pages-- do you know how that could be accomplished?

My feed url has a limit parameter (&limit=), which I want to set to a high number; leaving it blank defaults to 10.

I found a pagination class... my code currently looks like this:

if (!isset($_GET['page']) || !isnum($_GET['page'])) { $_GET['page'] = 1; }
        foreach ( $xmlObj ->results->result as $item )
{

$data_ref = $item->date;

if ( $tempCounter < 20 ) {
        echo "<a href="{$item -> url}" target="_blank">{$item -> jobtitle}</a><br />";
        if ($item->company)
        {echo "{$item -> company}";}
        if ($item->company && $item->city)
        {echo " - ";}
        if ($item->city)
        {echo "{$item -> city}<br />";}
        echo dateTimeDiff($data_ref)." From {$item -> source} <br />";
        echo "{$item -> snippet}<br /><br />";

    $tempCounter += 1;
}
}

class display {
function pagination($rows, $per_page, $current_page, $page_link) {
global $core,$C;

// Create a Page Listing
$this->pages = ceil($rows / $per_page);

// If there's only one page, return now and don't bother
if($this->pages == 1) {
return;
}

// Pagination Prefix
$output = "Pages: ";

// Should we show the FIRST PAGE link?
if($current_page > 2) {
$output .= "<a href="". $page_link ."?page=1" title="First Page"><<</a>";
}

// Should we show the PREVIOUS PAGE link?
if($current_page > 1) {
$previous_page = $current_page - 1;
$output .= " <a href="". $page_link ."?page=". $previous_page ."" title="Previous Page"><</a>";
}

// Current Page Number
$output .= "<strong>[ ". $current_page ." ]</strong>";

// Should we show the NEXT PAGE link?
if($current_page < $this->pages) {
$next_page = $current_page + 1;
$output .= "<a href="". $page_link ."?page=". $next_page ."" title="Next Page">></a>";
}

// Should we show the LAST PAGE link?
if($current_page < $this->pages - 1) {
$output .= " <a href="". $page_link ."?page=". $this->pages ."" title="Last Page">>></a>";
}
return $output;
}
}

$display = new display;
$rows='60';
if ($tempCounter >= 20){
echo $display->pagination($rows, "20", $_GET['page'], "jobs.php");
}

The page numbers are showing up right, but the same 20 listings show on every page. If I remove

if ( $tempCounter < 20 )

then it shows all listings on one page.

Any advice?

# Andrew at 3/11/2009 4:33 am cst

Thanks for the tutorial, got it working with an rss feed from the BBC website

# Steve at 8/12/2009 12:28 am cst

I found this and it was very helpful although I had to change the foreach loop to:

foreach ( $xmlObjTwitter->channel->item as $item )

and then all was good.

Cheers

# Peter Ohlsson at 1/4/2010 7:17 pm cst

Thanks - worked nicely, and a great resource on a site with a hosting service that does not allow "fopen(URL)".

-- Peter