Example: Using Caller Id in Asterisk
extensions.conf
This example uses extension #23. When someone calls extension #23, the caller id popup is activated

exten => 23,1,Ringing()
exten => 23,n,Wait(1)
exten => 23,n,Answer
exten => 23,n,AGI(cidlookup.php,${CALLERID},green)
;you may want to use a Dial() command here or convert to a macro to use on every inbound call
exten => 23,n,Hangup()

SQL create example data
This is the SQL script used to create the database and tables. The cidlookup is the database. The tables include lookup data (cidnum), the customer data (custlist), and customer represetentative list (replist). The tarball has example data included
--
-- Current Database: cidlookup
--

CREATE DATABASE IF NOT EXISTS cidlookup;

USE cidlookup;

--
-- Table structure for table cidnum
--

DROP TABLE IF EXISTS cidnum;
CREATE TABLE cidnum (
  cidactive			char(1)  NOT NULL default 'A',
  cidnum				char(11) NOT NULL default '12223334444',
  cidname				char(16) NOT NULL default 'unknown',
  cidcustid			char(12) NOT NULL default 'unknown',
  cidrepid			char(6)  NOT NULL default 'empty',
  PRIMARY KEY  (cidnum,cidname)
) ;

--
-- Table structure for table custlist
--

DROP TABLE IF EXISTS custlist;
CREATE TABLE custlist (
  cuactive			char(1)  NOT NULL default 'A',
  custid				char(12) NOT NULL default 'unknown',
  custconame		char(16) NOT NULL default 'unknown',
  custrepid			char(6)  NOT NULL default 'empty',
  PRIMARY KEY  (custid)
) ;


--
-- Table structure for table replist
--

DROP TABLE IF EXISTS replist;
CREATE TABLE replist (
  repactive			char(1)  NOT NULL default 'A',
  repid				char(12) NOT NULL default 'unknown',
  repfname		char(16) NOT NULL default 'unknown',
  replname		char(16) NOT NULL default 'unknown',
  reploginid	char(16) NOT NULL default 'unknown',
  PRIMARY KEY  (repid)
) ;


PHP script
This is the actual PHP script that does the work. First we have the Digium provided PHP AGI script that interacts with Asterisk. To this we add our PHP lookup routines that interact with the database and tables above, then we write out a file as a webpage that is the screen pop the user gets.

#!/usr/local/bin/php -q


	foreach($agi as $key=>$value)
	{
		$noop = 1;
		fwrite(STDERR," -- $key = $value\n");
		fflush(STDERR);
		echo " -- $key = >$value<\n";
		fputs($stdlog," -- $key = >$value<\n");
	}
	
	// indicate in the CLI> that we have launched the agi script
	fputs($stdlog,"VERBOSE \"Here we go!\" 2\n");
	echo "VERBOSE \"Here we go!\" 2\n";
	read();

	// any write/echo output from this script to the asterisk program must 
	// have a coresponding read or you will hang asterisk

	write("Call from ".$agi['channel']." - Calling phone");
	read();
	fputs($stdlog,"Call from ".$agi['channel']." - Called channel\n");
	fputs($stdlog,"Caller Number ".$agi['callerid']." - Caller Number\n");
	fputs($stdlog,"Caller Name ".$agi['calleridname']." - Caller Name\n\n");

	// this is what we expect for the caller id variables:
	//       agi_callerid: 18576870181
	//       agi_calleridname: JONES JACK J
	// Note: some caller id strings have a comma after the last name so
	//       if you parse this aware the string format is not standard

	// ok, lets get all the fields for this record
	$query="SELECT * from cidnum where cidnum='" . $agi['callerid'] . "';";
	fputs($stdlog,"query=$query<\n");
	$Result = mysql_query($query,$conn);
	$num_rows = mysql_num_rows($Result);
	if ($terrno == 0)
	{
		// a successful query, log the error
		$terrno = mysql_errno($conn);
		$errno = sprintf("%s",$terrno);
		fputs($stdlog," errno $errno\n");
	} else {
		// an unsuccessful query, log the failure so we can fix 
		// that at some point
		$terrno = mysql_errno($conn);
		$terror = mysql_error($conn);
		$errno = sprintf("%s",$terrno);
		$error = sprintf("%s",$terror);
		fputs($stdlog," errno $errno\n");
		fputs($stdlog," error $error\n");
	}

	// now, get the record details and place them into a web only variable
	// indicated by a preceding w_ 
	// this is used to distinquish a just retrieved database field from 
	// a webpage veriable on a refresh.  we can tell if the webpage changed 
	// and an update must be performed on the database
	// this would be used if the webpage had any editing capabilities
	$Row = mysql_fetch_array($Result, MYSQL_ASSOC);
	$w_cidnum       = $Row["cidnum"];
	$w_cidname      = $Row["cidname"];
	$w_cidcustid    = $Row["cidcustid"];
	$w_cidrepid     = $Row["cidrepid"];

	// check to make sure we only got one record  
	fputs($stdlog," num_rows=$num_rows\n");
	if ($num_rows == 1)
	{
		fputs($stdlog,"PASS: cidnum [$w_cidnum] returned exactly $num_rows row<\n");
	} else {
		fputs($stdlog,"ERROR: cidnum [$w_cidnum] returned $num_rows rows<\n");
	}

	// ok, now get the assigned customer rep id for this customer
	$query="SELECT * from replist where repid='" . $w_cidrepid . "';";
	fputs($stdlog,"query=$query<\n");
	$Result = mysql_query($query,$conn);
	$num_rows = mysql_num_rows($Result);

	// check to make sure we only got one record  
	if ($num_rows == 1)
	{
		fputs($stdlog,"PASS: reploginid [$w_reploginid] returned exactly $num_rows row<\n");
	} else {
		fputs($stdlog,"ERROR: reploginid [$w_reploginid] returned $num_rows rows<\n");
	}

	// get the record for our customer service rep
	$Row = mysql_fetch_array($Result, MYSQL_ASSOC);
	$w_repfname    	= $Row["repfname"];
	$w_reploginid    = $Row["reploginid"];

	// log what we currently have to the logfile
	fputs($stdlog,"cidnum=$w_cidnum<\n");
	fputs($stdlog,"cidname=$w_cidname<\n");
	fputs($stdlog,"cidcustid=$w_cidcustid<\n");
	fputs($stdlog,"cidrepid=$w_cidrepid<\n");
	fputs($stdlog,"reploginid=$w_reploginid<\n");

	// close db connection
	mysql_close($conn);

	// at some point, we can figure out how to route to the reps, 
	// primary rep first (if currently logged in)
	// secondary rep next or next rep logged in or place in the queue!

	// now create the screen pop web page

	// create the HTML ACTION parameters to put in the FORM ACTION= param 
	$ACTION_HOST = "192.168.15.81";
	$ACTION_DIR = "pop9";
	$ACTION_SCRIPT = "callpop4.php";

	// create the POP_URL to send to the rep
	// this is where the customer rep will point his/her browser
	// it just happens to be on the same host
	$POPHOST = "192.168.15.81";
	$POP_DIR = "pop9/scrnpop";

	// create the call alert (accept/reject) webpage to launch for this call
	$WEB_DIR  = "/var/www/html/";
	$WEB_DIR .= $POP_DIR;
	$now = date('Y-m-d-H-i-s.u');
	// create the filename and add the date to the filename so we can keep a 
	// record of pops although that is overkill
	$WEB_FILENAME = 'scrnpop_' . $now . "-" . $w_cidcustid . "-" . $w_reploginid . ".php";
	$WEBPAGE = $WEB_DIR . "/" . $WEB_FILENAME;
	fputs($stdlog,"WEB_DIR filename is >$WEB_DIR<\n");
	fputs($stdlog,"WEB_FILENAME is >$WEB_FILENAME<\n");
	fputs($stdlog,"WEBPAGE is >$WEBPAGE<\n");

	// now create the actual URL
	$POP_URL="http://" . $POPHOST . "/" . $POP_DIR . "/" . $WEB_FILENAME;
	fputs($stdlog,"POP_URL created is >$POP_URL<\n");
	fputs($stdlog,"\n");
	fputs($stdlog,"=====================\n");
	fputs($stdlog,"\n");

	...

logfile: my_agi.log

=======================================
Started at Sat Oct 27 0:40:06 EDT 2007
 -- request = >cidlookup.php<
 -- channel = >SIP/300-08852f98<
 -- language = >en<
 -- type = >SIP<
 -- uniqueid = >1193459995.77<
 -- callerid = >17673431234<
 -- calleridname = >Jack's Widget Company<
 -- callingpres = >0<
 -- callingani2 = >0<
 -- callington = >0<
 -- callingtns = >0<
 -- dnid = >47<
 -- rdnis = >unknown<
 -- context = >local-sip<
 -- extension = >23<
 -- priority = >4<
 -- enhanced = >0.0<
 -- accountcode = ><
VERBOSE "Here we go!" 2
Call from SIP/300-08852f98 - Called channel
Caller Number 17673431234 - Caller Number
Caller Name Jack's Widget Company - Caller Name

query=SELECT * from cidnum where cidnum='17673431234';<
 errno 0
 num_rows=1
PASS: cidnum [17673431234] returned exactly 1 row<
query=SELECT * from replist where repid='rep00';<
PASS: reploginid [dcantera] returned exactly 1 row<
cidnum=17673432234<
cidname=Jack's Widget Company<
cidcustid=custid00<
cidrepid=rep00<
reploginid=dcantera<
w_reploginid=dcantera<
WEB_DIR filename is >/web/pop9/scrnpop<
WEB_FILENAME is >scrnpop_2007-10-27-00-40-06.u-custid00-dcantera.php<
WEBPAGE is >/web/pop9/scrnpop/scrnpop_2007-10-27-00-40-06.u-custid00-dcantera.php<
POP_URL created is >http://192.168.15.81/pop9/scrnpop/scrnpop_2007-10-27-00-40-06.u-custid00-dcantera.php<


WEBPAGE=/web/pop9/scrnpop/scrnpop_2007-10-27-00-40-06.u-custid00-dcantera.php<

screen pop 'Call Alert' that is created

URL: /web/pop9/scrnpop/scrnpop_2007-10-10-00-19-22.u-custid00.php

This is the URL that you would send to the PC as a notification that a call is coming in to them (see example pop alert below). Once the rep accepts the call, the session variables are set with the customer key and a lookup is done on the customer database to pull up any other data and screens you desire. The PC should be running a small utility that is listening on a port for the PBX to provide the call alert URL to launch.



 New Call: custid00


New Call
Caller Number: 17673431234
Caller Name: Jack's Widget Company
Customer Id: custid00
Rep Id: rep00


Example tarball cid-example.tar
Right Click to download
consult the README file for installation notes.