note 107793 deleted from function.xml-parse-into-struct by danbrown
| From: | danbrown@php.net | Date: | Thu, 10 May 2012 18:27:30 +0000 |
| Subject: | note 107793 deleted from function.xml-parse-into-struct by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-187756@lists.php.net to get a copy of this message | ||
Note Submitter: CyberSEO.net
----
Here is my World's smallest (I hope so :)) RSS parser with optional caching capabilities.
<?php
// The World's smallest RSS feed parser by CyberSEO.net (contacts: http://www.cyberseo.net/#contacts)
// Put your RSS feed URL here:
$feed = "http://rt.com/news/today/rss/";
// 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 by 5. Change it to whatever you want.
$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";
}
xml_parser_free($xml_parser);
file_put_contents($filename, $content, LOCK_EX);
}
echo $content;
?>
Just place this code somewhere into your PHP file and it will parse the given feed. Perhaps somebody
will find it useful.