1 #!/usr/local/bin/php -q
2 <?php
3 /**
4 * @package phpAGI_examples
5 * @version 2.0
6 */
7
8 set_time_limit(30);
9 require('phpagi.php');
10
11 $agi = new AGI();
12 $agi->answer();
13
14 $agi->text2wav('Please enter your zip code.');
15 $result = $agi->get_data('beep', 3000, 5);
16 $search = $result['result'];
17
18 $db = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'weather.txt';
19 $min = 0; $max = filesize($db);
20 $fp = fopen($db, 'r');
21 do
22 {
23 $mid = floor(($min + $max) / 2);
24 fseek($fp, $mid);
25 fgets($fp, 4096);
26 list($zip, $city, $state, $latitude, $longitude, $station) = explode("\t", trim(fgets($fp, 4096)));
27 if($search < $zip)
28 $max = $mid;
29 elseif($search > $zip)
30 $min = $mid;
31 } while($max - $min > 1 && $zip != $search);
32 fclose($fp);
33
34 if($search != $zip)
35 $text = "I could not find the weather station for $zip";
36 else
37 {
38 $xml = join('', file("http://www.nws.noaa.gov/data/current_obs/$station.xml"));
39 $vals = $index = NULL;
40 $p = xml_parser_create();
41 xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
42 xml_parse_into_struct($p, $xml, $vals, $index);
43 xml_parser_free($p);
44 $i = 0;
45 $data = get_children($vals, $i);
46
47 $text = "The closes weather station to $city is at {$data['LOCATION']}. ";
48 $text .= "It is currently {$data['WEATHER']}. The temperature is {$data['TEMP_F']} degrees";
49 if($data['WIND_MPH'] > 0)
50 $text .= " with wind from the {$data['WIND_DIR']} at {$data['WIND_MPH']} miles per hour";
51 if($data['WINDCHILL_F'] != 'Not Applicable')
52 $text .= ". The wind chill is {$data['WINDCHILL_F']} degrees";
53 if($data['HEAT_INDEX_F'] != 'Not Applicable')
54 $text .= ". The heat index is {$data['HEAT_INDEX_F']}";
55 $text .= '.';
56 }
57
58 $agi->text2wav($text);
59
60 $agi->text2wav('Goodbye');
61 $agi->hangup();
62
63
64 function get_children($vals, &$i)
65 {
66 $ret = array();
67 for(++$i; $i < count($vals); $i++)
68 {
69 if(isset($vals[$i]['attributes']['NAME']))
70 $name = $vals[$i]['attributes']['NAME'];
71 else
72 $name = $vals[$i]['tag'];
73
74 if($name != '' && $vals[$i]['type'] == 'open')
75 $rt[$name][] = get_children($vals, $i);
76 elseif($vals[$i]['type'] == 'close')
77 {
78 if(isset($rt)) foreach($rt as $key=>$val)
79 {
80 if(count($val) == 1)
81 $ret[$key] = $val[0];
82 else
83 $ret[$key] = $val;
84 }
85 return $ret;
86 }
87 elseif($name != '')
88 {
89 if(isset($vals[$i]['attributes']['VALUE']))
90 $rt[$name][] = $vals[$i]['attributes']['VALUE'];
91 elseif(isset($vals[$i]['value']))
92 $rt[$name][] = $vals[$i]['value'];
93 }
94 }
95 return $ret;
96 }
97 ?>
Documentation generated on Wed, 20 Jul 2005 15:21:24 +0000 by phpDocumentor 1.2.3