Displaying RSS contents on a page using PHP

A while ago I set out to work finding out how to display the contents of an RSS feed onto a XHTML page. Grated there where lots that served up the contents using JavaScript and Flash. The main usage of this code lives on a local page that can not be accessed online. Its like my own little RSS reading area that is hidden away.

So here is the code that displays this blogs posts on a html page

$rss_url = "http://www.shanedj.com/blog/feed/rss/";
    if (!$rss_data = @file_get_contents($rss_url)) {
        echo "<p>No RSS found at $rss_url</p><p>Hmmm thats not meant to happen. RSS Fail!</p>";
    } else {

    $rss_xml = SimpleXML_Load_String($rss_data);

    $channel_title = $rss_xml->channel->title;
    $channel_link = $rss_xml->channel->link;

    foreach ($rss_xml->channel->item as $item) {
        $item_title = $item->title;
        $item_link = $item->link;
        $item_description = $item->description;
        echo "<h3><a href=\"$item_link\">$item_title</a></h3><p>$item_description</p><p class=\"text_right\"><a href=\"$item_link\">Continue reading  $item_title</a></p>\n";
        }
    }

The main part that displays the data on the page is right at the end

        echo "<h3><a href=\"$item_link\">$item_title</a></h3><p>$item_description</p><p class=\"text_right\"><a href=\"$item_link\">Continue reading  $item_title</a></p>\n";

You can change this however you want to display it in whatever fashion you want.

Values that you can echo to get data are as follows

$channel_title - This is the usually the blog name
$channel_link - This is usually a the blog url
$item_title - This is the title of a post
$item_link - This is the URL of the post
$item_description - This is the summary contents of the post

You can play around with displaying it however you want. For more information about playing with RSS (XML) with php. Bounce over to the XML section at PHP.net

Shane

Share and Enjoy:
  • TwitThis
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • LinkedIn
  • Slashdot
  • StumbleUpon
  • Technorati
  • Live
  • MySpace
  • Pownce
  • Reddit
  • Tumblr

If you liked this then check out these

Tags: ,

One Response to “Displaying RSS contents on a page using PHP”

  1. shanedj says:

    Would appreciate comments on this post, can I make any improvements etc?

    Thanks

Leave a Reply