Simple PHP and Asterisk dialplan…

asterisk-by-digiumIn This post I am just going to publish how to make a simple dial plan in asterisk by sending the call to PHP to process.

The application is just going to be the standard asterisk Milliwatt application which answers a call and plays a continues tone.

In asterisk (On an Ubuntu Server System), /etc/asterisk/extensions.con add

exten => 1701,1,Answer
exten => 1701,n,AGI(test.php)
exten => 1701,n,hangup(18)

In asterisk /usr/share/asterisk/agi-bin, create a file called test.php (# touch test.php then us nano to open/edit it # nano test.php) add the below in to the file:

#!/usr/bin/php -q
echo "EXEC NoCDR n";
echo "EXEC Answer n";
echo "EXEC PlayTones 1000 n";
echo "EXEC Wait 300 n";
echo "HANGUP n";

again in asterisl, in your default context add (include => Milliwait)

reload asterisk (asterisk -rx "reload")

from an extension dial 1701, and you should get a continuos tone.

This is just a simple way of passing a call to PHP to do some logic processing on it.

Asterisk can even send veriables to PHP, to capture these and use them in the test file you would use something like:

$agivars = array();
while (!feof(STDIN)) {
$agivar = trim(fgets(STDIN));
if ($agivar === '') {
$agivar = explode(':', $agivar);
$agivars[$agivar[0]] = trim($agivar[1]);

you can then use the below veriables in PHP to change the call flow etc-

  • agi_request – The filename of your script
  • agi_channel – The originating channel (your phone)
  • agi_language – The language code (e.g. “en”)
  • agi_type – The originating channel type (e.g. “SIP” or “ZAP”)
  • agi_uniqueid – A unique ID for the call
  • agi_version – The version of Asterisk (since Asterisk 1.6)
  • agi_callerid – The caller ID number (or “unknown”)
  • agi_calleridname – The caller ID name (or “unknown”)
  • agi_callingpres – The presentation for the callerid in a ZAP channel
  • agi_callingani2 – The number which is defined in ANI2 see Asterisk Detailed Variable List(only for PRI Channels)
  • agi_callington – The type of number used in PRI Channels see Asterisk Detailed Variable List
  • agi_callingtns – An optional 4 digit number (Transit Network Selector) used in PRI Channels see Asterisk Detailed Variable List
  • agi_dnid – The dialed number id (or “unknown”)
  • agi_rdnis – The referring DNIS number (or “unknown”)
  • agi_context – Origin context in extensions.conf
  • agi_extension – The called number
  • agi_priority – The priority it was executed as in the dial plan
  • agi_enhanced – The flag value is 1.0 if started as an EAGI script, 0.0 otherwise
  • agi_accountcode – Account code of the origin channel
  • agi_threadid – Thread ID of the AGI script (since Asterisk 1.6)

For more information, see

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS
This entry was posted in Little Guides and tagged , , , , , . Bookmark the permalink.

Comments are closed.