Read and RSS feed

With this function you can read any rss feed you want!

<?php

function readRSSFile($filename) {
        $data = implode('', file($filename));
        $parser = xml_parser_create();
        xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
        xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
        xml_parse_into_struct($parser, $data, $values, $tags);
        xml_parser_free($parser);

        foreach ($tags as $key => $val) {
                if ($key == 'item') { // select the 'items'
                        $itemRanges = $val;
                        for($i=0; $i<count($itemRanges); $i+=2) {
                                $offset = $itemRanges[$i] + 1;
                                $len = $itemRanges[$i + 1] - $offset;
                                $result[] = combineItem(array_slice($values, $offset, $len));
                        }
                }
        }
        return $result;
}
function combineItem($itemValues) {
        for ($i=0; $i<count($itemValues); $i++) {
                if(isset($itemValues[$i]['value'])) {
                        $item[$itemValues[$i]['tag']] = $itemValues[$i]['value'];
                }
        }
        return $item;
} 



//To Read an RSS file
$latest = readRSSFile("RssFeedHere.rss");
for($i=0; $i<count($latest); $i++) {
        print'<p>'.$latest[$i]['description'].'</p>';
} 

?>

Usage

Copy the two above functions to your includes file.
And use the second snippet to ready any RSS feed you want!
Have fun!


Comments

Rolf
Posted on 18.07.2010 13:38

You can use the function in an other var.
In the code above i used $latest = readRSSFile("");
You can use another like: $otherVar = readRSSFile("");
And than in the for loop use $otherVar.

guest
Posted on 18.07.2010 13:25

if i want in the same time to parse two rss feeds then how we can do it ?

Add your comment