How to Display RSS Feeds from a Website in PHP

I didn’t need this particular piece of code till I started customizing my SMF mobile version and had to show recent topics on the homepage using PHP. Now this doesn’t apply to SMF alone, its just a PHP code you can use in any script so far you know just how to do it. You can grab the RSS of any website and display it on another site and this is dead simple even if you have no idea about PHP coding.

This is quite useful if you’ve got more than one site and wish to show updates on one of your sites to visitors on another site. Depending on how you want it, you can customize the display by altering this code or just using it as-is:

<?php
$rss = new DOMDocument();
$rss->load('http://www.9jaclub.com/index.php?action=.xml;type=rss;sa=news;limit=20;boards=3,10,11,14,15,16,47,50,55,2,4,6,7,23,33,24,26,21,32,36,29,27,31,28,25,30,57,51,38,39,40');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
echo '<p><strong><div class="titlebg">Latest 10 Topics</div></strong><br />';
$limit = 10;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<div class="rssdisplay"><strong><li> <a href="'.$link.'" title="'.$title.'">'.$title.'</a></li></strong>'.$date.'</div>';
}
?>

You should change http://www.9jaclub.com/index.php?action=.xml;type=rss;sa=news;limit=20;boards=3,10,11,14,15,16,47,50,55,2,4,6,7,23,33,24,26,21,32,36,29,27,31,28,25,30,57,51,38,39,40 to your own RSS feed.

Of course, if you’re inserting this into an existing PHP file, you should remove the first and last line. Alternatively, you can copy the code to a text file, rename as rss.php and upload to your domain root. You can include anywhere it in your PHP document with include('rss.php');.

Customizing the elements and layout

The code above only displays the RSS title and the date but you can as well add the description. To include the description, you may add '.$description.' just before the closing div tag where the date is shown or simply replace '.$date.' with '.$ddescription.' before that same closing div tag.

You can also style it with CSS by defining rssdisplay CSS class in your style sheet

avatar
Don Caprio is a full time blogger / web designer living in Nigeria and apart from blogging, my other obsession is music. I'm a self-confessed Tupac fan, an iOS lover, an Android user, a Windows 8 power user, a writer with a 5-year old unfinished novel... and the list goes on and on. You can add me on Facebook, follow my updates on Twitter or add me to your circles on ! You can as well grab my feed and get updates through email.
avatar

@doncaprio

Tech blogger at http://doncaprio.com Web designer at http://doncaprio.org
How To Minimize Android Data Consumption http://t.co/wYoJAApeWf - 4 hours ago
avatar
avatar

Get free daily updates through email

Comments

  1. I used to follow your blog with the RSS feeds thanks for Operamini browser. For a mobile user like me I can easily subscribed with the help of its reader pre-installed.
    Michael recently posted..Animals Color Guard — Mimicry for Protection and FoodMy Profile

  2. avatar david mark says:

    cool bro, but what if someone needs to grab the post body? Without leaving the site?

  3. lovely post, but i found something missing in this particular great post, which is the screenshots. i love it when i see you make screenshots to illustrate in your posts. But overall its still easy to digest.
    Ahmsta recently posted..Top 10 Games for the New iPadMy Profile

  4. Great post, thanks for sharing this :) it’s really a big help for me..exactly this code is what i need and also may i use this blog as a guidelines? big THANKS..
    Ronald Harris recently posted..HP Envy 17-3270NR 17.3-Inch Laptop (Silver)My Profile

  5. Wow thank you for the code. I have been having this hige problem about the RSS feed…this is really helpful to me. Thank you so much!

  6. Hi Don !
    Thank you for this great tutorial. I have been facing RSS feed problem to my blog, it will be really helpful for me.
    Googma Sansar recently posted..The Effective Ways to Web OptimizationMy Profile

Speak Your Mind

*

CommentLuv badge