Use PHP to display any RSS feed on your site Sep18 '07

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:

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:

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

Other resources

Categories: Code , PHP , RSS , Tutorials , Web Development , XML

Add Feedback (view all)

Leave feedback

Feedback

Input format: The editor controls below will assist with Markdown syntax.

Status

Sub-status

Your info

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

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 undef ... Read more.

It would be awsome if it worked. ... Read more.

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

Thanks you Jon! That worked great. ... Read more.

Very helpful, thanks! ... Read more.

matthom is published and produced by Matt Thommes - an independent publishing enthusiast, mobile blogger, content creator, informative writer, web developer from Chicago. Never one to conform, Matt intends to promote the effect the web has on our lives, in an effort to intensify, instruct, and clarify all that is happening around us.

Contact Matt

Similar Entries

Stats

2178 unique visits since August 2008

Syndicate

Advertisements