[Asterisk-Users] AGI and PHP
Florian Overkamp
florian at obsimref.com
Tue Nov 11 00:30:34 MST 2003
Hi,
At 13:48 11-11-2003 +0800, you wrote:
>i made a lot of silly mistakes along the way which could have been
>avoided if only there were some kinda howto or samples. at the risk
>of looking stupid, i decided to shared my experience in hopes that
>it might help some newbie get going with PHP.
I've done this a long time ago, and by sheer accidence someone posed a
question about it yesterday. Below are some of the comments I sent him.
By the way, I also have a reusable AGI function library in PHP. I think I
posted an older version to the list a long long time ago, but if there is
interest I can clean it up once more and put it out there... Using normal
function calls instead of the handywork saves the programmer from nasty
little bugs :-))
---*SNIP*---
First of all, never sleep() in AGI, if you need timeouts, do a WAIT FOR
DIGIT or something, otherwise you might end up deadlocking your asterisk
server.
Secondly, after issueing a command, read back the results, that way you can
be sure your command finished and you can even get some input about what
happened. Asterisk posts back result codes too...
Thirdly, don't forget newlines where they are needed.
I've adjusted your script accordingly and added some functions to ensure
some of these things:
#!/usr/bin/php4 -q
<?php
ob_implicit_flush(true);
set_time_limit(0);
$in = fopen("php://stdin","r");
// toggle debugging output (more verbose)
$debug = false;
// Do function definitions before we start the main loop
function read() {
global $in, $debug;
$input = str_replace("\n", "", fgets($in, 4096));
if ($debug) echo "VERBOSE \"read: $input\"\n";
return $input;
}
function errlog($line) {
global $err;
echo "VERBOSE \"$line\"\n";
}
function write($line) {
global $debug;
if ($debug) echo "VERBOSE \"write: $input\"\n";
echo $line."\n";
}
// parse agi headers into array
while ($env=read()) {
$s = split(": ",$env);
$agi[str_replace("agi_","",$s[0])] = trim($s[1]);
if (($env == "") || ($env == "\n")) {
break;
}
}
// main program
errlog("Call from ".$argv[1]." - Calling phone");
read();
write("SAY DIGITS 22 X"); // X is the escape digit. since X is not DTMF, no
exit is possible :-)
read();
write("SAY NUMBER 2233 X"); // X is the escape digit. since X is not DTMF,
no exit is possible :-)
read();
// clean up file handlers etc.
fclose($in);
exit;
?>
---*SNIP*---
best regards,
Florian
More information about the asterisk-users
mailing list