note 107713 deleted from function.xml-parse by salathe
| From: | salathe@php.net | Date: | Thu, 08 Mar 2012 09:17:44 +0000 |
| Subject: | note 107713 deleted from function.xml-parse by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-186541@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 = file_get_contents ( "http://rt.com/news/today/rss/" );
// There must be a local path to your optional cache file (e.g. cache.txt) which must be writable by
the script.
$filename = "cache.txt";
// The default cash refresh time is set to 1 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, $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";
}
file_put_contents ( $filename, $content, LOCK_EX );
xml_parser_free ( $xml_parser );
}
echo $content;
?>
Just place this code somewhere into your PHP file and it will parse the given feed. Perhaps somebody
will find it useful.