AGI/FASTAGI how does it work?

Ive read all the wiki’s and help files, and read cgi which is like it I guess.
I know cgi works on the webserver, a client in Internet Explorer could go to an html/php page that would call the cgi program, then the cgi program would run and report the data back to the client in Internet explorer.

but on agi and fastagi im confused.
I know you can execute the agi/fastagi from the extensions.conf if a user dials a certain extension like so:
exten => 444,18,AGI(|${intime})­
or fastagi
exten => 5551212,1,AGI(agi://|${EXTEN}|${UNIQUEID}|${CALLERID(name)})

-FastAGI just runs the AGI script on another asterisk server instead of on the server it is originated from?
-Could I run a script using internet explorer? EXAMPLE:
go to this in the browser

What im trying to do:
i got a php file on my personal webserver/sql. The php file connects to the mysql database and runs a query and returns the results. Just simply done with form and submit button. The sql database contains a list of account numbers and customer info

On my asterisk server, when a caller calls they enter thier account number and i store it as a variable.

When i clicked submit on my php page, i would like it to retrieve the account number variable from asterisk and run the query using it.

This seems so simple yet i have read sooooo much on everything and cannot figure out how to do it.
One other problem i just do SetGlobalVar in the dialplan to set the variable,
exten => 902,n,SetGlobalVar(NUMBERVAR=${NUMBER})
and I figured out how to reference it using ajam, but to do it i had to know the channel id 082214a0 which is different every call: … =NUMBERVAR

please help!

no one?

I picked up my perl book and am reading that…seems to interface nicely with asterisk…not sure if it will help in this scenario or not

im currently installing orderlycalls, it seems like it could be what i need. however thier is like next to no information on it unless your already a developer…im still not positive

AGI is fairly well documented on the web, specifically when it comes to PHPAGI.

While you could use a form and POST the data to it, it is a messy solution. You are best to just do all the work within a single file.

Lets say you want to collect a 4 digit account from the caller, and return a matching 4 digit number from a MySQL Database.


<?php require('phpagi.php'); $agi = new AGI(); $username="yourun"; $password="yourpwd"; $database="yourdb"; $dialedacc = (isset($argv[1])) ? $argv[1]; $accnum = substr($dialedacc, 1); mysql_connect("localhost",$username,$password); @mysql_select_db($database) or die ("Unable to select DB"); $query="SELECT dbacc FROM accounts WHERE accnum = '$accnum'"; $result=mysql_query ($query); while($dbacc=mysql_fetch_array($result)){ $customeraccount=$dbacc[0]; $agi->set_variable($customeraccount, $customeraccount); exit; } exit; ?>


This is expecting you to pass a variable ${dialedacc} to it, and in turn will set the variable ${customeraccount}. You would obviously want to write error checking, and the code could do with a bit of cleaning up, nor have I actually tested it.

Note that PHPAGI is a PHP Class that does not come with the Asterisk source, you can find it at

so the code you posted, lets say it is named codeIposted.php

this file is located on my laptop (IP, which is also running apache/php/mysql.

then thier is the asterisk server on its own computer.

So I could:
first set the variable ${ACCTNO} to a 4 digit account number.
then dial:
exten => 1234,1,AGI(agi://|${ACCTNO})

In that codeIposted.php it has:
$dialedacc = (isset($argv[1])) ? $argv[1];

So ${ACCTNO} would become argv[1]

and it could finish querrying the dB yadda yadda yadda.

then to show the results id have to:
echo $dbacc[‘0’];
i think, although im not sure what
$agi->set_variable($customeraccount, $customeraccount);
is doing…

and im not sure how this is going to display it on someones screen automatically? sorry im new at phpagi and somewhat at php/mysql. been reading about the phpagi classes…

i appreciate your time to help me

There is no real need to host / call this within Apache per se when I’m assuming you are talking about a small number of calls. Simply drop the file into your Asterisk AGI directory (Typically /var/lib/asterisk/agi-bin/) then call it as;

exten => 1234,1,Read(ACCTNO,,4) exten => 1234,n,AGI(codeIposted.php,${ACCTNO})

An echo isn’t going to do much more then possibly show up in the CLI when PHPAGI has debugging enabled (I think, have not tested it).

Set_Variable is doing just as it indicates, creating a channel variable of ${customeraccount} that you can call from within the Asterisk dialplan.

For example you could;

which would set the incoming name of the caller to the account number you pulled from the DB.

When you say you want this number to show up on their screen, are you talking PC screen or the LCD on the phone?

ok that all makes sense.

yea i want it to show up on thier computer screen.
it would show all the info for that account that is in the mysql database.

this is the part that im having trouble finding information on how to do.
it seems that orderlycalls is a java/asterisk integrated development environment that would do things like this. They created orderlyQ with it.
But i cannot find anything on orderlycalls for begginers so im not making much progress thier yet.

unless you know of a way to make it work?
ps…btw agi is just allowing the dialplan to interact with another programing language (this case being php).
fastagi allows it to do the same thing but on another server to take a load off of the asterisk server.

that makes sense to me now.