improved caching/parsing XML with SimpleXML
Source code
1
<?php include('./.config.php');
2
3 require('Cache/Lite/Output.php');
4 $id = 'yahoo.oddlyenough.simpleXML';
5
6 $options = array(
7 'cacheDir' => './cache/',
8 'lifeTime' => 3600 // 1 hour
9 );
10 $cache = new Cache_Lite_Output($options);
11
12 if (strcasecmp($_SERVER['QUERY_STRING'],'remove')==0) {
13 $cache->remove($id) ;exit("cache purged");
14 }
15
16 if (!($cache->start($id))) {
17
18 include('./.fetch_xml_with_socket.php5');
19
20 if ($rss = simplexml_load_string(@fetchRss("rss.news.yahoo.com","/rss/oddlyenough"))) {
21 foreach ($rss->channel->item as $item) {
22
23 printf('<p><strong><a href="%s">%s</a></strong><br />%s</p>',
24 $item->link,
25 $item->title,
26 $item->description);
27
28 }
29 $cache->end(); // the buffered output is now stored into a cache file
30 echo "<p>......used fresh data and cached it........</p>";
31
32 } else { // it failed for some reason
33
34 echo "<p>rss feed not available</p>";
35 }
36
37 } else {
38
39 echo "<p>.... and I got it from the cache, how cool is that? (-:</p>" ;
40 }
41 include('./.footer.php')?>
Output
Warning: require(Cache/Lite/Output.php) [function.require]: failed to open stream: No such file or directory in /home/davidmintz/www/davidmintz.org/php_course/examples/10/simpleXML_rss_cached.improved.php5 on line 3
Fatal error: require() [function.require]: Failed opening required 'Cache/Lite/Output.php' (include_path='/usr/home/dmintz/inc/pear:.:/usr/local/lib/php:/usr/local/php5/lib/pear') in /home/davidmintz/www/davidmintz.org/php_course/examples/10/simpleXML_rss_cached.improved.php5 on line 3