Have a Snippet?

Keep, track and share your code snippets with your friends

PHP: RSS feed parser/reader Share on Vkontakte

The World's smallest RSS feed parser. Create an empty chache.txt file and chmod it 666 to enable the built-in caching.

// Put your RSS feed URL here. E.g.:
$feed = "http://habrahabr.ru/rss/best/";
// Assign the local path to your optional cache file (e.g. cache.txt) which must be writable by the script.
$filename = "cache.txt";
// The default cache refresh time is set to one hour (60 * 60), but you can change it to any other value.
if (file_exists($filename) && filesize($filename) && (time() - filectime($filename)) < 60 * 60) {
    $content = file_get_contents($filename);
} else {
    $xml_parser = xml_parser_create();
    xml_parse_into_struct($xml_parser, file_get_contents($feed), $vals, $index);
    // The maximum number of displayed items is limited with 5. Change it to whatever you want.
    $cnt = min(5, count($index["TITLE"]), count($index["DESCRIPTION"]));
    $content = "";
    for ($i = 1; $i < $cnt; $i++) {
        $content .= "

" . html_entity_decode($vals[$index["TITLE"][$i]]["value"], ENT_QUOTES) . "

\n"; $content .= "

" . html_entity_decode($vals[$index["DESCRIPTION"][$i]]["value"], ENT_QUOTES) . "

\n"; } xml_parser_free($xml_parser); file_put_contents($filename, $content, LOCK_EX); } echo $content;


Tag: RSS, parser, feed

0 Comments