Page 244 - Asterisk™: The Future of Telephony
P. 244
$s = split(":",$temp);
$name = str_replace("agi_","",$s[0]);
$agi[$name] = trim($s[1]);
}
Next, we’ll read in all of the AGI variables passed to us by Asterisk. Using the fgets
command in PHP to read the data from STDIN, we’ll save each variable in the hash called
$agi. Remember that we could use these variables in the logic of our AGI script, al-
though we won’t in this example.
# print all AGI variables for debugging purposes
foreach($agi as $key=>$value)
{
fwrite(STDERR,"-- $key = $value\n");
fflush(STDERR);
}
Here, we print the variables back out to STDERR for debugging purposes.
#retrieve this web page
$weatherPage=file_get_contents($weatherURL);
This line of code retrieves the XML file from the National Weather Service and puts
the contents into the variable called $weatherPage. This variable will be used later on
to extract the pieces of the weather report that we want.
#grab temperature in Fahrenheit
if (preg_match("/<temp_f>([0-9]+)<\/temp_f>/i",$weatherPage,$matches))
{
$currentTemp=$matches[1];
}
This section of code extracts the temperature (in Fahrenheit) from the weather report,
using the preg_match command. This command uses Perl-compatible regular expres-
#
sions to extract the needed data.
#grab wind direction
if (preg_match("/<wind_dir>North<\/wind_dir>/i",$weatherPage))
{
$currentWindDirection='northerly';
}
elseif (preg_match("/<wind_dir>South<\/wind_dir>/i",$weatherPage))
{
$currentWindDirection='southerly';
}
elseif (preg_match("/<wind_dir>East<\/wind_dir>/i",$weatherPage))
{
$currentWindDirection='easterly';
}
elseif (preg_match("/<wind_dir>West<\/wind_dir>/i",$weatherPage))
{
$currentWindDirection='westerly';
}
# The ultimate guide to regular expressions is O’Reilly’s Mastering Regular Expressions, by Jeffrey E.F. Friedl.
216 | Chapter 9: The Asterisk Gateway Interface (AGI)