Asterisk 1.2 + PHP 5.2: Why does PHP AGI not work?

I have Asterisk 1.2 and PHP5.2 installed in AS4.

Asterisk is work well. And then I wanted to add some AGI scripts based PHP, refer to voip-info.org/wiki/view/Asterisk+AGI+php. But the script doesn’t work. Following is the file. Even /var/log/asterisk/my_agi.log was not created. Call this script in extension.conf by “exten => 866XXXXXXX,5,AGI(agi-abc.php)”. The file can be found in /var/lib/asterisk/agi-bin/

#!/usr/local/bin/php -q

<?php ob_implicit_flush(true); set_time_limit(6); $in = fopen("php://stdin","r"); $stdlog = fopen("/var/log/asterisk/my_agi.log", "w"); // toggle debugging output (more verbose) $debug = false; // 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 errlog($line) { global $err; echo "VERBOSE \"$line\"\n"; } 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 == "") || ($env == "\n")) { break; } } // main program echo "VERBOSE \"Here we go!\" 2\n"; read(); errlog("Call from ".$agi['channel']." - 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); fclose($stdlog); exit; ?>

[root@localhost php-5.2.0]# /usr/local/bin/php -v
PHP 5.2.0 (cli) (built: Dec 21 2006 23:01:37)
Copyright © 1997-2006 The PHP Group
Zend Engine v2.2.0, Copyright © 1998-2006 Zend Technologies

can you be more specific than ‘it doesn’t work’?

Following is debug information from asterisk cli. It looks that “#!/usr/local/bin/php -q” makes no sense.

– Launched AGI Script /var/lib/asterisk/agi-bin/agi-abc.php
AGI Tx >> agi_request: agi-abc.php
AGI Tx >> agi_channel: IAX2/58.34.240.203:44165-1
AGI Tx >> agi_language: en
AGI Tx >> agi_type: IAX2
AGI Tx >> agi_uniqueid: 1166804520.41
AGI Tx >> agi_callerid: 218254120
AGI Tx >> agi_calleridname: 218.25.41.20
AGI Tx >> agi_callingpres: 0
AGI Tx >> agi_callingani2: 0
AGI Tx >> agi_callington: 0
AGI Tx >> agi_callingtns: 0
AGI Tx >> agi_dnid: 013504018203
AGI Tx >> agi_rdnis: unknown
AGI Tx >> agi_context: AnyB2C
AGI Tx >> agi_extension: 013504018203
AGI Tx >> agi_priority: 3
AGI Tx >> agi_enhanced: 0.0
AGI Tx >> agi_accountcode:
AGI Tx >>
AGI Rx << Usage: php [options] [-f] [–] [args…]
AGI Tx >> 510 Invalid or unknown command
AGI Rx << php [options] -r [–] [args…]
AGI Tx >> 510 Invalid or unknown command
AGI Rx << php [options] [-B <begin_code>] -R [-E <end_code>] [–] [args…]
AGI Tx >> 510 Invalid or unknown command
AGI Rx << php [options] [-B <begin_code>] -F [-E <end_code>] [–] [args…]
AGI Tx >> 510 Invalid or unknown command
AGI Rx << php [options] – [args…]
AGI Tx >> 510 Invalid or unknown command
AGI Rx << php [options] -a
AGI Tx >> 510 Invalid or unknown command
AGI Rx << CLI>
AGI Tx >> 510 Invalid or unknown command
AGI Rx << -a Run interactively
AGI Tx >> 510 Invalid or unknown command
AGI Rx << -c | Look for php.ini file in this directory
AGI Tx >> 510 Invalid or unknown command
AGI Rx << -n No php.ini file will be used
AGI Tx >> 510 Invalid or unknown command
AGI Rx << -d foo[=bar] Define INI entry foo with value 'bar’
AGI Tx >> 510 Invalid or unknown command
AGI Rx << -e Generate extended information for debugger/profiler
AGI Tx >> 510 Invalid or unknown command
AGI Rx << -f Parse and execute .
AGI Tx >> 510 Invalid or unknown command
AGI Rx << -h This help
AGI Tx >> 510 Invalid or unknown command
AGI Rx << -i PHP information
AGI Tx >> 510 Invalid or unknown command
AGI Rx << -l Syntax check only (lint)
AGI Tx >> 510 Invalid or unknown command
AGI Rx << -m Show compiled in modules
AGI Tx >> 510 Invalid or unknown command
AGI Rx << -r Run PHP without using script tags <?..?>
AGI Tx >> 510 Invalid or unknown command
AGI Rx << -B <begin_code> Run PHP <begin_code> before processing input lines
AGI Tx >> 510 Invalid or unknown command
AGI Rx << -R Run PHP for every input line
AGI Tx >> 510 Invalid or unknown command
AGI Rx << -F Parse and execute for every input line
AGI Tx >> 510 Invalid or unknown command
AGI Rx << -E <end_code> Run PHP <end_code> after processing all input lines
AGI Tx >> 510 Invalid or unknown command
AGI Rx << -H Hide any passed arguments from external tools.
AGI Tx >> 510 Invalid or unknown command
AGI Rx << -s Display colour syntax highlighted source.
AGI Tx >> 510 Invalid or unknown command
AGI Rx << -v Version number
AGI Tx >> 510 Invalid or unknown command
AGI Rx << -w Display source with stripped comments and whitespace.
AGI Tx >> 510 Invalid or unknown command
AGI Rx << -z Load Zend extension .
AGI Tx >> 510 Invalid or unknown command
AGI Rx <<
AGI Tx >> 510 Invalid or unknown command
AGI Rx << args… Arguments passed to script. Use – args when first argument
AGI Tx >> 510 Invalid or unknown command
AGI Rx << starts with - or script is read from stdin
AGI Tx >> 510 Invalid or unknown command
AGI Rx << CLI>
AGI Tx >> 510 Invalid or unknown command
AGI Rx << --rf Show information about function .
AGI Tx >> 510 Invalid or unknown command
AGI Rx << --rc Show information about class .
AGI Tx >> 510 Invalid or unknown command
AGI Rx << --re Show information about extension .
AGI Tx >> 510 Invalid or unknown command
AGI Rx << CLI>
AGI Tx >> 510 Invalid or unknown command
– AGI Script agi-abc.php completed, returning 0

Why???

I got it.

Because unix and dos file format, it’s different.

You have a php syntax error on this part:

$agi[str_replace(“agi_”,"",$s[[0])] = trim($s[[1]);

it should be

$agi[str_replace(“agi_”,"",$s[0])] = trim($s[1]);

try executing it on the console or on the command line and it will tell you the error.