Working with Asterisk AGI and PHP

Working with Asterisk AGI and PHP

Surprisingly, the Asterisk AGI is not difficult to learn at all.

We stuck with php as the programming language since it seemed as the easiest language to integrate with the AGI.

As sometimes may be the case, Linux does not play friends with many Microsoft products. Just my luck, their CRM system was using a MS SQL database. No problem I said, I mean how hard can it be to access MS SQL from php?


First, you have to enable the MS SQL module in php. Unfortunately if you are using CentOS, there is no yum repository install for this (at the time of this post); you have to manually recompile php.

After it has been installed we can now finally move to good stuff.

Start by creating a new file inside the /var/lib/asterisk/agi-bin folder. Let’s call it myapp.php.

Remember, we are programming the AGI using php.

Also, you need to chmod 777 , chown asterisk:asterisk and chmod +x the file or asterisk will not be able to execute it.

In the beginning of the file we MUST have the following line:

#!/usr/bin/php -q

This is to let php now we will NOT be parsing any HTML.

The following lines will be the standard opening and closing of php script. All php code must be within these opening and closing statements.



(without the dots)

We then need to include the phpagi.php files as it provides essential functions for the AGI.


Lets begin the nitty gritty by creating a new instance of the AGI.

$agi = new AGI();

And then we answer the call:


This is basically what you need for for any script you decide to create. If in the asterisk dialplan (I’ll talk about that later) you answer the call first, then you do not need the above line. It won’t make the server blow up, I’m just letting you know its not required. 🙂

What the customer wanted was to prompt the caller for their account number and if they had any outstanding invoices we needed to add up all the balances and tell the caller how much they owed.

So what do we need first? Well, obviously we need to ask the user for their account number!
How can we ‘ask the user?’ I’m glad you asked. There are two ways (well, really three) on how to do this:

1.) Use Flite (a asterisk text-to-speech engine)
2.) Pre-record your voice using Asterisk and play the file
3.) Use Flite to convert a text file to a sound file.

I went with #2 as it sounds more professional than using flite, however; because their balance would always be random I needed to use flite just to ‘say’ the balance outloud.

So let’s continue with the code:

//Ask the user for their account number
$result = $agi->get_data(‘custom/please-enter-your-id’, 3000, 15);
$customerID = $result[‘result’];

Wow, hold on there! What does this all mean?!

An array variable called $result is created which stores the digits returned from the AGI command get_data. So what the get_data command does is play a sound file (which was pre-recorded) and listens for DTMF digits pressed by the caller. So how long does it wait for and how many digits can the user press? This is determined by the two numbers after the comma. A timeout was set to after 3000MS (milli-seconds) and the maximum number of digits is 15. You can change this at your discretion. Another variable called $customerID was created which pulls the result from the array. We now have the digits the caller has pressed inside the variable $customerID!

So now all you have to do is query the database with the $customerID variable and see how many invoices are unpaid and count the balance.

Because we now have php compiled with MS SQL support this will be a breeze.

This is how you do it:

$myServer = “”;
$myUser = “sa”;
$myPass = “ursapassword”;
$myDB = “dbNAME”;
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass) or
die(“Couldn’t connect to SQL Server on $myServer”);
//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle) or die(“Couldn’t
open database $myDB”);

while ($numRows==0) {

$result = $agi->get_data(‘custom/please-enter-your-ac-num, 3000, 15);
$customerID = $result[‘result’];

//Send a query and see if they entered a valid license.
$query = “SELECT * FROM tbl_customers WHERE CustomerID=’$customerID’”;

//Execute the query
$result = mssql_query($query);
//Query has been sent, it should return 1
$numRows = mssql_num_rows($result);

//Ok, the user has entered a correct accout number

//Now check the see if they have any invoices

$query = “SELECT * FROM tbl_invoices WHERE Status=’Unpaid’ AND CustomerID=’$customerID’”;

//execute the SQL query and return records
$result = mssql_query($query);

//get number of rows
$numRows = mssql_num_rows($result);

if ($numRows == 0) {
//No invoices for this customer
$agi->exec(“Flite”,”\”You have no outstanding invoices\””);

else {
//count them up!
while($row = mssql_fetch_array($result))
//Add the balance of all the invoices
$balance = $balance + $row[“Balance”];
$agi->exec(“Flite”,”\”Your current balance is $balance\””);

//close the connection and recordset objects freeing up resources

$agi -> hangup($request[‘agi__channel’]);

WOW, finally we are done!

What we have done is connect to a MS SQL database, and issue a query using the account number we received. Once we received a valid account number we checked to see if they had any invoices that were unpaid by counting how many rows were returned. If it was 0 we just hang up on the caller (how rude!) but before we do, we let them know there were no outstanding invoices. If they had more than 0 invoices then we counted the balance on each one and used flite to ‘say’ their balance. After it has been said, we hang up on them. 🙂 You can obviously make it more polite by saying “Thank you for calling, GoodBye”, but where is the fun in that?

Hopefully this gets people started on the right track.

Popular Post

New Fiber Internet Service in Coral Gables

New Fiber Internet Service in Coral Gables

Welcome to a new kind of Internet: ITP Fiber Launc
Read More
Working with Asterisk AGI and PHP

Working with Asterisk AGI and PHP

Surprisingly, the Asterisk AGI is not difficult to
Read More
DAHDI_SPANCONFIG failed on span 1: No such device or address (6)

DAHDI_SPANCONFIG failed on span 1: No such device or address (6)

Trying to upgrade Asterisk and DAHDI drivers and c
Read More


Need help ?

Follow Us

Subscribe to Our Newsletter