note 107713 added to function.xml-parse
| From: | CyberSEO.net | Date: | Tue, 28 Feb 2012 16:01:26 +0000 |
| Subject: | note 107713 added to function.xml-parse | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186414@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 69.147.83.197
Probable Submitter: 78.108.183.101
----
Manual Page -- http://www.php.net/manual/en/function.xml-parse.php
Edit -- https://master.php.net/note/edit/107713
Del: integrated -- https://master.php.net/note/delete/107713/integrated
Del: useless -- https://master.php.net/note/delete/107713/useless
Del: bad code -- https://master.php.net/note/delete/107713/bad+code
Del: spam -- https://master.php.net/note/delete/107713/spam
Del: non-english -- https://master.php.net/note/delete/107713/non-english
Del: in docs -- https://master.php.net/note/delete/107713/in+docs
Del: other reasons-- https://master.php.net/note/delete/107713
Reject -- https://master.php.net/note/reject/107713
Search -- https://master.php.net/manage/user-notes.php