Display RSS with PHP
247   1  

Display RSS with PHP

  18 Sep 2018
  Coding Category
Display RSS with PHP

  25% more discounted by using the Giliapps as code in HostGator

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 RSS feed.

$rss = new DOMDocument();


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(' & ', ' &amp; ', $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>';


by Bandicoot Marketing

Author: Bandicoot Marketing

Do you like it? Share it with your friends.