+321 123 4567
info@test.com
  • Home
  • My Services
    • Web Design
    • Web Development
    • Technical Writing
  • Portfolio
  • My Thoughts
  • Free Tools
    • Photo Editor
    • Avatar Generator
    • Email Migration Tool
    • QR Code Generator
    • RSS to HTML Converter
    • IP Address Geolocation
    • Base64 Image Encoder
    • NATO Translator
    • All Tools
  • Contact
Home
HTML / PHP
How to Display RSS Feeds from a Website in PHP

How to Display RSS Feeds from a Website in PHP

Wale Adekile
January 28, 2013

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



Previous Post
NCSettings: The Best Alternative to SBSettings on iOS 6.0.1 / 6.1
Next Post
How to Run Firefox OS On your Windows PC
Wale Adekile
Husband to the world's most beautiful woman and father to two awesome kids. A nerd, obviously; also, a technical business analyst, a web designer who preaches the gospel of WordPress, and a writer. You can check out my published books on Amazon.
FacebookHTML / PHP

Facebook Share Button Stopped Working? Here’s a Quick Fix

13 years ago
HTML / PHP

Integrating Facebook Comment on Dynamic PHP Web Pages

13 years ago

Recent Articles

wahl rapic clip hair clipper
Wahl Rapid Clip Hair Clipper Review: DIY Made Easy
BenQ LaptopBar Review: Luxury or Necessity?
yunmai easepro unboxed
Yunmai EasePro Massage Gun Review
g20
Teendow G20 Robot Vacuum and Mop Review
bathroom
Dear Men, Spraying All Over the Toilet Seat is Disgusting and Irresponsible
winx mediatrans
How to Transfer Photos from iPhone to Computer Without iTunes
google workspace
How to Migrate Emails From Google Workspace/G Suite to cPanel
synology diskstation ds920+
Synology DiskStation DS920+ Review
playstation 4
Why I haven’t Upgraded to the PlayStation 5 Yet
pain
Started Watching Anime as an Adult? You’re Not Alone

All contents on this blog are copyright protected and cannot be reproduced on other websites without permission.

Copyright © 2022 – NerdieDad | Home | Contact | About | Privacy Policy