Retrieving an RSS Feed in PHP
Posted by Wez | Filed under PHP, Text Tutorials
RSS Feeds are widely used throughout the internet today and on all kinds of websites. From websites like Digg to most blogs. If you want to display an RSS feed on your website its probably easier than you think.
For this example I'm going to show you how to display the RSS feed for The Tutorial Blog using PHP.
The Code
The first two lines of code that we write are going to be what retrieve the RSS feed.
$xml = new SimpleXMLElement($file);
Now the next thing we do is use the foreach function so for each item found do something with it
What are code will do is simply echo the titles of all the items found within the feeds. If you also want to include a link to the feed you can do this by printing to screen $feed->link.
All the code together should look something like this.
$file = file_get_contents("http://www.thetutorialblog.com/feed/");
$xml = new SimpleXMLElement($file);
foreach($xml->channel->item as $feed){
echo $feed->title.'<br>';
}
?>
Thanks for taking the time to read this tutorial. I hope you have learned something.


August 15th, 2008 at 10:12 pm
Simple and useful. Thanks.
August 19th, 2008 at 7:47 pm
Brilliant. I have looked so hard for a good tutorial on getting contents of an RSS feed as opposed to making one!
September 19th, 2008 at 7:17 am
Very useful and practical tutorial..
http://www.codebounce.com/php
September 30th, 2008 at 11:44 am
Great tutorial. Thanks