PHPAGI is not working

1- I navigate to
#cd /var/lib/asterisk/agi-bin/
2- I create php script name writetofile.php below in above path
#!/usr/bin/php -q

<?php $myfile = fopen("newfile.txt", "a") or die("Unable to open file!"); $txt = "Mickey Mouse\n"; fwrite($myfile, $txt); $txt = "Minnie Mouse\n"; fwrite($myfile, $txt); fwrite($myfile,date('Asia/Jerusalem')); fwrite($myfile,"\n"); fclose($myfile); echo date('Asia/Jerusalem'); echo "\n"; ?>

3- #chmod 777 writetofile.php
4- #dos2linux writetofile.php
5- # vim newfile.txt
6- newfile.txt is empty
7- # php writetofile.php
8- File has text
Mickey Mouse
Minnie Mouse
AM1340am/JAsia/JerusalemWed, 27 Apr 2016 11:40:13 +030000000013amWednesdayAsia/Jerusalem04
9- # cd /etc/asterisk/
10- # vim extensions_custom.conf
11- Add the context below on the file extensions_custom.conf
exten => 8010,1,Answer()
same => n,AGI(writetofile.php)
same => n,NoOp(writetofile)
same => n,Hangup()
12- # asterisk –vvvvvvr
13- localhost*CLI> dialplan reload
14- I’m try to call 8010
15- Debug result as below
[0K<SIP/8043-00000016>AGI Tx >> agi_callerid: 8043
<SIP/8043-00000016>AGI Tx >> agi_calleridname: Ahmad Hoshya
<SIP/8043-00000016>AGI Tx >> agi_callingpres: 0
<SIP/8043-00000016>AGI Tx >> agi_callingani2: 0
<SIP/8043-00000016>AGI Tx >> agi_callington: 0
<SIP/8043-00000016>AGI Tx >> agi_callingtns: 0
<SIP/8043-00000016>AGI Tx >> agi_dnid: 8010
<SIP/8043-00000016>AGI Tx >> agi_rdnis: unknown
<SIP/8043-00000016>AGI Tx >> agi_context: from-internal
<SIP/8043-00000016>AGI Tx >> agi_extension: 8010
<SIP/8043-00000016>AGI Tx >> agi_priority: 2
<SIP/8043-00000016>AGI Tx >> agi_enhanced: 0.0
<SIP/8043-00000016>AGI Tx >> agi_accountcode:
<SIP/8043-00000016>AGI Tx >> agi_threadid: -1244038288
<SIP/8043-00000016>AGI Tx >>

[0K<SIP/8043-00000016>AGI Rx << AM2948am/JAsia/JerusalemWed, 27 Apr 2016 11:48:29 +030000000029amWednesdayAsia/Jerusalem04
<SIP/8043-00000016>AGI Tx >> 510 Invalid or unknown command

[0K – <SIP/8043-00000016>AGI Script writetofile.php completed, returning 0

Note :Asterisk version 11.19.0 and PHP version is PHP 5.3.28 (cli)

I don’t have much experience with AGI scripts but I think you should try this:

exten => 8010,1,Answer()
same => n,Set(result=${SHELL("/usr/bin/php -q /var/lib/asterisk/agi-bin/writetofile.php")})
same => n,NoOp(${RESULT})
same => n,Hangup()

Also, you have to change the owership of the writetofile.php using this commands:

chown asterisk /var/lib/asterisk/agi-bin/writetofile.php

chgrp asterisk /var/lib/asterisk/agi-bin/writetofile.php

If that doesn’t help, try this or read this

Do you have SELinux enabled? Often SELinux will block this kind of access to scripts.

i tried change owner (chown) and change group (change group) to asterisk but same problem still apear.

ls -ltr writetofile.php

-rwxrwxrwx 1 asterisk asterisk 311 Apr 26 21:54 writetofile.php

note SELinux is disabled.

cat /etc/sysconfig/selinux

This file controls the state of SELinux on the system.

SELINUX= can take one of these three values:

enforcing - SELinux security policy is enforced.

permissive - SELinux prints warnings instead of enforcing.

disabled - SELinux is fully disabled.


SELINUXTYPE= type of policy in use. Possible values are:

targeted - Only targeted network daemons are protected.

strict - Full SELinux protection.


also no result apear after execute above context and the debug result as below
– Executing [8011@from-internal:1] Answer(“SIP/8043-00000002”, “”) in new stack
– Executing [8011@from-internal:2] Set(“SIP/8043-00000002”, “result=”) in new stack
– Executing [8011@from-internal:3] NoOp(“SIP/8043-00000002”, “”) in new stack
– Executing [8011@from-internal:4] Hangup(“SIP/8043-00000002”, “”) in new stack

please your help.

I’m geeting this error right now
Tx >> agi_threadid: 47079559997760
<SIP/8043-0000000c>AGI Tx >>
<SIP/8043-0000000c>AGI Rx << 2016/04/30 04:02:10pm
<SIP/8043-0000000c>AGI Tx >> 510 Invalid or unknown command
[2016-04-30 16:02:10] ERROR[25713][C-0000000a]: utils.c:1393 ast_carefulwrite: write() returned error: Broken pipe
– <SIP/8043-0000000c>AGI Script writetofile.php completed, returning 0
– Executing [8010@from-internal-custom:4] NoOp(“SIP/8043-0000000c”, " this is writetofile.php agi scrip") in new stack.
please your help

The “Broken pipe” comes from the AGI-script which does not meet the requirements when used together with asterisk.
You have at least to ensure that all Input from Asterisk is correctly fetched by Your AGI-Script. For an example You may look here: