Upgrade 3CX to v18 and get it hosted free!

Asterisk simple php lookup mysql database to set callerid name

Author image

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

extensions.conf:

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_connect($dbhost,$dbuser,$dbpass);
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
        $row=mysql_fetch_array($row);
        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

Related Posts:

Get 3CX - Absolutely Free!
Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.