Asterisk simple php lookup mysql database to set callerid name

This script use the AGI to lookup a telephone number in a mysql database created by the user to set the name of the caller

Requires phpagi (phpagi-asmanager.php phpagi-fastagi.php phpagi.php in script folder), php and mysql


exten => 100,1,AGI,/path/to/php/script/lookup
exten => 100,n,Set(CALLERID(name)=${lookupcid})
exten => 100,n,Dial(SIP/203,20,t)
exten => 100,n,Hangup

lookup script:

#!/usr/bin/php -q
require 'phpagi.php';
$agi = new AGI();

$no=preg_replace("#[^0-9]#","",$agi->request[agi_callerid]);//remove any non numeric characters

connect to "asterisk" database in mysql; in this case only 1 table "clients"
column telsearch contains concatenated mobile, home and work numbers
e.g. 0121777887207986238123
script will search for caller number within this field
$db = 'asterisk';
$dbuser = 'xxx';
$dbpass = 'xxx';
$dbhost = 'localhost';

mysql_select_db("$db"); //or die("could not open database");
$row=mysql_query("select title,firstname,lastname from clients where telsearch like '%$no%' LIMIT 1");
if (mysql_num_rows($row)==1&&strlen($no)>4){//if found number and number greater than 4 digits to avoid 3 digit internal extensions
        if ($row[title]) $name .= $row[title]." ";
        if ($row[firstname]) $name .= $row[firstname]." ";
        if ($row[lastname]) $name .= $row[lastname];
else $name=$agi->request[agi_callerid];//else set calleridname to callerid number
$agi->set_variable("lookupcid", $name);

Similar contributions

Gigahz' fork of this, with online lookup if local fails
Asterisk simple php lookup up callerid name from Horde Turba
Created by: johnnyrevell, Last modification: Mon 20 of May, 2013 (20:26 UTC) by xj25vm
Please update this page with new information, just login and click on the "Edit" or "Discussion" tab. Get a free login here: Register Thanks! - Find us on Google+