Retrieving an RSS Feed in PHP Comments (9)
by Wez Pyke - August 4, 2008 in 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.

$file = file_get_contents("http://www.thetutorialblog.com/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
foreach($xml->channel->item as $feed){
echo $feed->title.'<br>';
}

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.

<?php

$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.

Bookmark and Share
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • email
  • FriendFeed
  • LinkedIn
  • Ping.fm
  • Tumblr
  • Twitter

Related posts:

  1. Twitter-like pagination using CodeIgniter and jQuery If you use Twitter you'll notice that there is a...
  2. Twitter-like pagination using CakePHP and jQuery I have already done this tutorial with CodeIgniter and jQuery...
  3. Adding security to CodeIgniter forms with a custom library class The class that we are going to create within CI...
  4. PHP gallery with no page reloads using jQuery For this tutorial we will be using the ImgBrowz0r class...

Related posts brought to you by Yet Another Related Posts Plugin.

Your Ad Here

9 Responses to “Retrieving an RSS Feed in PHP”

  1. box says:

    Simple and useful. Thanks.

  2. supersword says:

    Brilliant. I have looked so hard for a good tutorial on getting contents of an RSS feed as opposed to making one!

  3. Unreal Media says:

    Great tutorial. Thanks :)

  4. Marty says:

    Thank you for this! So many other tutes provide far more detail than I am after.

    If I could just ask one question - how can I wrap the title in its link so people can click on it?

    Cheers

  5. Marty says:

    Never mind :) Thank you!

  6. Adam says:

    Genius just the snip i was looking for

  7. Haris says:

    Cool One..!! Bt there is no Linking Function in it

  8. Great Tutorial want to learn more about xml and php.

Leave a Reply