note 110190 added to function.xml-parse-into-struct
| From: | CyberSEO | Date: | Fri, 28 Sep 2012 11:52:00 +0000 |
| Subject: | note 110190 added to function.xml-parse-into-struct | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-191062@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 = "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.
----
Server IP: 69.147.83.199
Probable Submitter: 173.214.255.81
----
Manual Page -- http://www.php.net/manual/en/function.xml-parse-into-struct.php
Edit -- https://master.php.net/note/edit/110190
Del: integrated -- https://master.php.net/note/delete/110190/integrated
Del: useless -- https://master.php.net/note/delete/110190/useless
Del: bad code -- https://master.php.net/note/delete/110190/bad+code
Del: spam -- https://master.php.net/note/delete/110190/spam
Del: non-english -- https://master.php.net/note/delete/110190/non-english
Del: in docs -- https://master.php.net/note/delete/110190/in+docs
Del: other reasons-- https://master.php.net/note/delete/110190
Reject -- https://master.php.net/note/reject/110190
Search -- https://master.php.net/manage/user-notes.php