An Easy Way to Display an RSS Feed with PHP
by c.bavota | Posted in Tutorials | 18 comments
RSS feeds are everywhere, and sometimes it’s a good idea to display one to keep people in the loop of important posts from your site, or sites you think might be relevant. Luckily, PHP 5 introduced the DOM extension which make it easy to work with XML documents. Now all it takes is just a small bit of code to fetch and display a feed.
The following code will first create a new DOMDocument() into which we will load the WordPress.org RSS feed.
$rss = new DOMDocument();
$rss->load('http://wordpress.org/news/feed/');
Then we will single out certain elements and place them into an array. For this example, I will just fetch the title, description, link and published on date.
$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);
}
Finally, we set it to display 5 posts on screen with the titles linking directly to the original post.
$limit = 5;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
}
Put it all together and this is what you get:
<?php
$rss = new DOMDocument();
$rss->load('http://wordpress.org/news/feed/');
$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);
}
$limit = 5;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
}
?>
Not too complicated. All you need to change is the feed you want to load (line #3) and the number of posts to display (line #14). Of course, you could always play around with the output to get it styled exactly how you want. That is totally up to you.



Thanks, it works great.
Excellent blog entry! Really useful! Keep it up
Exaclty what I was looking for, thank you. I’ve used it to pull in a feed from an external website which has very flaky performance so I’ve added an if else statement to return an error message if the value of $feed if null
load('http://www.informaworld.com/ampp/rss~content=t71610072'); $feed = array(); foreach ($rss->getElementsByTagName('item') as $node) { $item = array ( 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, 'link' => $node->getElementsByTagName('link')->item(0)->nodeValue, ); array_push($feed, $item); } $limit = 10; if ($feed == null) echo "does not compute"; else for($x=0;$x<$limit;$x++) { $title = str_replace("&", "&", $feed[$x]["title"]); $link = $feed[$x]["link"]; echo '<a href="'.$link.'" title="'.$title.'" rel="nofollow">'.$title.'</a>'; } ?>Thanks in advance for providing the script and for the answer.
How to limit the description text?
Date is not displaying correct. How to fix?
this is brilliant! – but I am a bit clueless… ummm how do I use my current style-sheet with this?
Thanks again for the article
doah! sorted that one! (just put a link to the style sheet) but, now.. how do I get a link in the RSS to open in a new window? .. sigh..
J
Add a
target="_blank"to the anchor tag.hi
nice job..sorry my english is low..
i want show just 5 characters at feed description..
—-how to limit characters to show description–
echo ”.$description.”;
tnx. im wait 4 answer
This should do it:
$description = explode(' ', $description, 5); $description = implode(" ",$description);hi .tnx for answer ,
i cant replace codes
can u make make something for me?
1- page encoding utf-8
2- look this rss link http://www.cbi.ir/ExRatesRss.aspx = i want to show just first 5 number (or 5 character`s)
i want like this demo
http://453831.20upload.net/files/1389/bahman/Capturevbi.png
yellow colored.
thank you for helping me..
$rss = new DOMDocument(); $rss->load('http://www.cbi.ir/ExRatesRss.aspx'); $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); } $limit = 5; for($x=0;$x<$limit;$x ) { $title = str_replace(' & ', ' & ', $feed[$x]['title']); $link = $feed[$x]['link']; $description = $feed[$x]['desc']; $date = date('l F d, Y', strtotime($feed[$x]['date'])); echo '<p>'.$description.'</p>'; }Add this right after the
$description = $feed[$x]['link'];line.thank very much.
i do with your great help thanks..
Hi,
I have tried as well to add the
$description = substr($description, 0, 5);
line and it works fine to control the lenght of the description. However I would like that the last word get’s cut in the middle and also add the usual three dots at the end.
What code lines should I add and where?
hi, article is exelent, but i need to retrive this two elements to, one is img and one is price, but it dose not work for me,
cant get this working corecly, can u sugest anything???
Great post, thanks! I’ve added the code to my theme but a few people using it have run into this error:
Parse error: syntax error, unexpected T_OBJECT_OPERATOR, expecting ‘)’ …
on this line of code:
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,I can’t replicate the error. Someone on this thread suggested it might be a conflict with GoDaddy, but someone with 1&1 hosting reported the same issue. Any ideas? Thanks.
The issue is probably the server not having the proper libraries installed in PHP.