So i tried using one of the examples liste don that page - When run - the CLI shows:
– Launched AGI Script /var/lib/asterisk/agi-bin/test3.agi
– AGI Script test3.agi completed, returning 0
The actual code:
#!/usr/bin/php -q
<?php
ob_implicit_flush(true);
set_time_limit(6);
error_reporting(0); //added by user: democritus
$in = fopen("php://stdin","r");
$stdlog = fopen("/var/log/asterisk/my_agi.log", "w");
// toggle debugging output (more verbose)
$debug = true;
// Do function definitions before we start the main loop
function read() {
global $in, $debug, $stdlog;
$input = str_replace("\n", "", fgets($in, 4096));
if ($debug) fputs($stdlog, "read: $input\n");
return $input;
}
function write($line) {
global $debug, $stdlog;
if ($debug) fputs($stdlog, "write: $line\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 == "") {
break;
}
}
write("GET VARIABLE foo");
$a = read();
echo "VERBOSE \"$a\" \n";
read();
// clean up file handlers etc.
fclose($in);
fclose($stdlog);
exit;
?>
[quote=“davevg”]I’m pretty sure the issue you are having here is that you need to read in all of the buffered input prior to doing your write. The agi process sends a bunch of variables on startup.
The following link gives an example. voip-info.org/tiki-index.php … 0AGI%20php[/quote]
This is a very good point. You need to read everything up to the point of your write before you try and read again otherwise the internal php pointer will be in the ‘wrong’ place.
Don’t forget ‘set agi debug on’ so you can see what your script is doing.
This worked like a charm! Thank you Wimnat! One last problem I cannot seem to be able to pass more than one variable through - How do you pass more than one?
// Get the variable I set in Asterisk
write(“GET VARIABLE foo”);
$myVarInPhp = read();
$stringArray = explode("(", $myVarInPhp);
$string = substr($stringArray[1], 0, -1);
echo “VERBOSE “$string” \n”;
// Get the second variable I set in Asterisk
write(“GET VARIABLE foo2”);
$myVarInPhp = read();
$stringArray = explode("(", $myVarInPhp);
$string = substr($stringArray[1], 0, -1);
echo “VERBOSE “$string” \n”;
You’ve ran an asterisk command ‘VERBOSE’ without reading its return value inbetween your two GET commands. When you try and read your GET command it’s actually reading your VERBOSE command.
Easy fix. Just put read() after echo “VERBOSE “$string” \n”;
Easier fix. Use errlog($string). The errlog function that i wrote automatically does a read() every time it’s called so you don’t have to worry about the read() every time.