Show any RSS feed in your website

By in PHP, Tutorials, Web on March 25, 2010

If you go to my site and click on the small RSS button near the clock, you’ll see a menu pop out with the list of my resent blog posts.
rss image
This I accomplished with a simple PHP function.Earlier I used feed burner for this but somehow I wanted to get rid of the logo they append to the list.
A small php function to get an RSS feed display in your website.The TagName may vary with types of blogging engine used.I made this for WP.

function populateFeeds(){
        //url of the rss feed
        $rssUrl = "http://www.rajesharma.com/blog/feed";
        $rss = file_get_contents($rssUrl);
	$xml = new DOMDocument();
	$xml->loadXML($rss);
 
         //for wordpress we get the elements with 'item' tag
	$list = $xml->getElementsByTagName('item');
	$i=0;
	foreach ($list AS $element)
	{
		$result[$i]['title'] = $element->getElementsByTagName('title')->item(0)->nodeValue;
		$result[$i]['link'] = $element->getElementsByTagName('link')->item(0)->nodeValue;
		$i++;
	}
 
	$html = '';
	foreach($result as $feed){
		$html .= '<li><a href="'.$feed['link'].'" target="_blank">'.$feed['title'].'</a></li>';	
	}
 
	echo $html;
}

And the HTML :

<div class="rssSub">
            <ul>
               <?php populateFeeds();?>
            </ul>
        </div>

Tags: , ,

2 Responses to “Show any RSS feed in your website”

  1. sujan says:

    k ho rajesh website ta jhan khatara bhako raicha
    dami cha …..keep it up man.

    Liked it.

Leave a Reply