Use PHP to display any RSS feed on your siteOver 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:
RSS feedFirst, 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:
Below is an example "node" of this RSS feed: PHPWith 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 ResultThis code will loop through each RSS feed
Nice, clean, and looks like it comes straight from my own database. Other resources
Comments/Mentions# 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? # 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 ) # 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 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:
The page numbers are showing up right, but the same 20 listings show on every page. If I remove
then it shows all listings on one page. Any advice? Thanks for the tutorial, got it working with an rss feed from the BBC website I found this and it was very helpful although I had to change the foreach loop to:
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 |
Recent Comments
Recent Music Listens
|
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