Wednesday, August 24, 2011

Function to Convert RSS to Array using PHP from RSS URL

Working with RSS feed is more usefull to promote your website as well as you can get relevant content for your website from another website using RSS Feed, Let us write a code  how to to get RSS feed content from RSS URL.
 


Function to Create RSS Details to PHP Array 




<?php


function rss_to_array($tag, $array, $url) {
	$doc = new DOMdocument();
	$doc->load($url);
	$rss_array = array();
	$items = array();
	foreach($doc->getElementsByTagName($tag) AS $node) {    
			foreach($array AS $key => $value) {
					$items[$value] = $node->getElementsByTagName($value)->item(0)->nodeValue;
			}
			array_push($rss_array, $items);
	}
	return $rss_array;
}

        $rss_tags = array(
                'title',
                'link',
                'guid',
                'comments',
                'description',
                'pubDate',
                'category',
        );
        $rss_item_tag = 'item';
        $rss_url = 'http://feeds.nytimes.com/nyt/rss/HomePage';
        
        $rssfeed = rss_to_array($rss_item_tag,$rss_tags,$rss_url);
        
echo '<pre>';
print_r($rssfeed);


?>

No comments:

Post a Comment