note 111375 deleted from function.xml-parse-into-struct by danbrown
| From: | danbrown@php.net | Date: | Tue, 05 Mar 2013 16:05:26 +0000 |
| Subject: | note 111375 deleted from function.xml-parse-into-struct by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-193908@lists.php.net to get a copy of this message | ||
Note Submitter: CyberSEO.net
----
Here is the World's smallest (I hope so :)) RSS parser with caching.
// Put your RSS feed URL here:
$feed = "http://rss.cnn.com/rss/cnn_topstories.rss";
// Assign the local path to your optional cache file
$filename = "cache.txt";
// The default cache refresh time (e.g. 1 hour)
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);
xml_parser_free($xml_parser);
// The maximum number of displayed items (e.g. 5).
$cnt = min(5, count($index["TITLE"]));
$content = "";
for ($i = 1; $i < $cnt; $i++) {
$content .= "<h3>" .
html_entity_decode($vals[$index["TITLE"][$i]]["value"], ENT_QUOTES) .
"</h3>\n";
$content .= "<p>" .
html_entity_decode($vals[$index["DESCRIPTION"][$i]]["value"], ENT_QUOTES) .
"</p>\n";
}
file_put_contents($filename, $content, LOCK_EX);
}
echo $content;