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:
- 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.
- 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.)
- 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
- Paul Stamatiou: How To: Parse XML with PHP5.
Categories: Code
, PHP
, RSS
, Tutorials
, Web Development
, XML ![]()
Add Feedback (view all)
Leave feedback
Consider reading the site disclaimer before commenting.
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.
Hi Matt, Thanks for the snippet...works great except you should update the twitter rss structure to: foreach ( $xmlObjTwitter -> status as $item ) ... Read more.
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 ... Read more.
Thanks for the tutorial, got it working with an rss feed from the BBC website ... Read more.
I found this and it was very helpful although I had to change the foreach loop to: foreach ( $xmlObjTwitter->channel->item as ... Read more.
Oh, man, you saved me a bunch of time, your rss to php is brilliant! Great, great resource! Thank you! ... Read more.
Thanks - worked nicely, and a great resource on a site with a hosting service that does not allow "fopen(URL)". -- Peter ... Read more.
Hi, I'm trying your method within a TextPattern istallation - therefore between ~ and getting the following error: Par ... Read more.