SimpleXML: parsing XML weather data
Source code
1
<?php include('./.config.php');
2 require('Cache/Lite.php');
3 $id = 'weather.gov_data_current_obs_KEWR.xml';
4
5 $cache = new Cache_Lite(array(
6 'cacheDir' => './cache/',
7 'lifeTime' => 600
8 ));
9
10 // for demo purposes, purge the cache if we are accessed
11 // with ?remove
12
13 if (strcasecmp($_SERVER['QUERY_STRING'],'remove')==0) {
14 $cache->remove($id) ;
15 echo ("cache purged... ");
16 }
17
18 // get xml, either live or cached
19 if ($data = $cache->get($id)) {
20 echo "using cached data...<br />";
21 //echo $data;
22 } else {
23 echo "no cache, fetching from network...<br />";
24 $ch = curl_init("http://weather.gov/data/current_obs/KEWR.xml");
25 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
26 $data = curl_exec($ch);
27 curl_close($ch);
28 if (!$cache->save($data)) {
29 die( "damn! could not write to cache" ); }
30 }
31 $xml = simplexml_load_string($data);
32 echo "<p>temperature at EWR $xml->temp_f Farenheit ($xml->observation_time)</p>";
33 include('./.footer.php')?>
Output
Warning: require(Cache/Lite.php) [function.require]: failed to open stream: No such file or directory in /home/davidmintz/www/davidmintz.org/php_course/examples/10/weather_service.php5 on line 2
Fatal error: require() [function.require]: Failed opening required 'Cache/Lite.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/weather_service.php5 on line 2