Php AGI For Asterisk

It is nice to customize the telephony system to meet your needs. If you are willing to learn more.. here I am writing this small tutorial to describe how to create a php agi to customize the asterisk telephony engine as you like.

This little agi connects to database retrieves the value and announces it. Rest……..Imagination is the limit…….

Go to /var/lib/asterisk/agi-bin

create a folder  #mkdir nishant

now lets create a php file nishant.php

!/usr/bin/php -q

answer(); //Lets connect to a database and announce the values: $hostname=”dbhost”; $dbname=”database_name”; $username=”database_user”; $password=”database_password”; $agi->text2wave(“Welcome  to my Demo Application”); // or you can play sound file. mysql_connect($hostname,$username,$password) or die (mysql_error()); mysql_select_db($dbname) or die(mysql_error()); $agi->text2wav(“Please enter Id:”); $id=$agi->text_input(‘UPPERCASE’); $agi->text2wav(“You entered $id”); $query=”your sql query goes here”; $result = mysql_query($query); $num=mysql_num_rows($result); if($num > 0) { $row=mysql_fetch_assoc($result); $agi->say_number($row[id]); }else $agi->text2wave(“No Records Found”); ?>

If text2wave doesnot work , make sure you have festival installed.

rpm -qa | grep festival

if not , install festival > yum install festival

then go to /etc/asterisk/phpagi.conf

and set path of festival to /usr/bin/text2wave

Other Tips and Tricks

$cid=$agi->get_variable(“CALLERID(name)”);
$cidname=strtoupper($cid[“data”]);
$agi->text2wav(“Hello,$cidname”);