Page 246 - Asterisk™: The Future of Telephony
P. 246
fwrite(STDOUT,"STREAM FILE with \"\"\n");
fflush(STDOUT);
$result = trim(fgets(STDIN,4096));
checkresult($result);
fwrite(STDOUT,"STREAM FILE $currentWindDirection \"\"\n");
fflush(STDOUT);
$result = trim(fgets(STDIN,4096));
checkresult($result);
fwrite(STDOUT,"STREAM FILE wx/winds \"\"\n");
fflush(STDOUT);
$result = trim(fgets(STDIN,4096));
checkresult($result);
fwrite(STDOUT,"STREAM FILE at \"\"\n";)
fflush(STDOUT);
$result = trim(fgets(STDIN,4096));
checkresult($result);
fwrite(STDOUT,"SAY NUMBER $currentWindSpeed \"\"\n");
fflush(STDOUT);
$result = trim(fgets(STDIN,4096));
checkresult($result);
fwrite($STDOUT,"STREAM FILE miles-per-hour \"\"\n");
fflush(STDOUT);
$result = trim(fgets(STDIN,4096));
checkresult($result);
}
Now that we’ve collected our data, we can send AGI commands to Asterisk (checking
the results as we go) that will deliver the current weather conditions to the caller. This
will be achieved through the use of the STREAM FILE and SAY NUMBER AGI commands.
We’ve said it before, and we’ll say it again: when calling AGI commands, you must
pass in all of the required arguments. In this case, both STREAM FILE and SAY NUMBER
commands require a second argument; we’ll pass empty quotes escaped by the back-
slash character.
You should also notice that we call the fflush command each time we write to
STDOUT. While this is arguably redundant, there’s no harm in ensuring that the AGI
command is not buffered and is sent immediately to Asterisk.
function checkresult($res)
{
trim($res);
if (preg_match('/^200/',$res))
{
if (! preg_match('/result=(-?\d+)/',$res,$matches))
{
fwrite(STDERR,"FAIL ($res)\n");
fflush(STDERR);
return 0;
}
else
{
fwrite(STDERR,"PASS (".$matches[1].")\n");
fflush(STDERR);
218 | Chapter 9: The Asterisk Gateway Interface (AGI)