AGI scripts with PHP

I’m trying to write my first script AGI with PHP.

My script, test1.agi in /var/lib/asterisk/agi-bin is:

#!/usr/bin/php -q

<?php set_time_limit(60); ob_implicit_flush(false); error_reporting(0); $stdlog = fopen("/var/log/asterisk/my_agi.log", "w"); fputs($stdlog, "ok!!!\n"); fclose($stdlog); return(100); ?>

From shell, using /usr/bin/php test1.agi, the script works fine. It create the file and write in the string “ok!!!”.

I create the following lines in extensions.conf:

exten => 711,1,Answer()
exten => 711,2,AGI(test1.agi)

When i call 711, in Asterisk log i see:

Executing Answer(“SIP/408-ad5b”, “”) in new stack
Executing AGI(“SIP/408-ad5b”, “test1.agi”) in new stack
Launched AGI Script /var/lib/asterisk/agi-bin/test1.agi
AGI Script test1.agi completed, returning 0

Therefore the script do not create the file my_agi.log.

I enabled the PHP log and it is working fine. If i run a wrong script i see the error in the log. If i run same script from Asterisk (calling 711) no error appear in PHP log.

Can you help me?


are you sure the script has privileges to write in that directory when run from asterisk?

with ps -aux i see /usr/sbin/asterisk is running as root

Owner of /var/log/asterisk is root with rwxr-xr-x rights

Is this incorrect?


From what I can see here, your program has a extension of “agi” not “php”. I do not believe the php interpreter wil pick this up and run it without a “php” extension.

Try changing the program name and make sure it is executable. Also, depending on what editor you are using you may want to run the dos2unix utility against the file prior to running it. Some windows editor programs put ugly control characters in the file that can also preventing it to run properly.

Good Luck.