caching/parsing XML with DOM
Source code
1
<?php include('./.config.php');
2 require('Cache/Lite/Output.php');
3 $id = 'yahoo.oddlyenough.DOM';
4
5 $cache = new Cache_Lite_Output( array(
6 'cacheDir' => './cache/', 'lifeTime' => 3600
7 ));
8
9 if (strcasecmp($_SERVER['QUERY_STRING'],'remove')==0) {
10 $cache->remove($id) ;
11 exit("cache purged");
12 }
13
14 if (!($cache->start($id))) {
15
16 include('./.fetch_xml_with_socket.php5');
17 $data = fetchRss("rss.news.yahoo.com","/rss/oddlyenough");
18 $dom = DOMDocument::loadXML($data);
19
20 foreach ($dom->getElementsByTagname('item') as $item) {
21
22 // get the title node
23 $title = $item->getElementsByTagname('title');
24 // and its text value
25 $title_text_value = $title->item(0)->firstChild->nodeValue;
26
27 // ditto for the description
28 $description = $item->getElementsByTagname('description');
29 $description_text_value = $description->item(0)->firstChild->nodeValue;
30
31 // convenience before legibility: grab the link in one line
32 $link = $item->getElementsByTagname('link')->item(0)->firstChild->nodeValue;
33
34 print "<p><a href=\"$link\">$title_text_value</a><br />$description_text_value</p>\n";
35 }
36
37 $cache->end(); // the buffered output is now stored into a cache file
38 echo "<p>......used fresh data......... </p>";
39
40 } else {
41 echo "<p>.... and I got it from the cache, how cool is that? (-:</p> " ;
42 }
43 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/cached_rss_with_DOM.php5 on line 2
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/cached_rss_with_DOM.php5 on line 2