[HOWTO] Using Google & PHP to get CallerID Name w/o AGI

This is a quick, dirty, and easy way to get CallerID Name information if all that your VoIP provider gives you is the CallerID number. The PHP script uses Google’s phonebook feature to do a reverse lookup on the phone number. If a matching name isn’t found, it then looks up the caller’s area code to at least give you “Michigan Call”, “California Call”, etc.

Asterisk simply calls this webpage on incoming calls by using the CURL function.

caveats:

  • Google’s reverse number database isn’t as complete as I wish it were, but it’s better than nothing.
  • Only works on US numbers.
  • Only works with Asterisk 1.2 or higher
  • Works by “screen scraping” Google: If they modify how the search results are returned, it will break the script. This may or may not violate Google’s Terms of Service… I dunno. But this probably shouldn’t be used for anything other than experimenting for personal use.
  • I don’t do much in the way of bonds checking, and don’t do any error trapping. (see: the previous caveat)

get_cidname.php:

<?php
echo trim(getCIDName("http://www.google.com/search?hl=en&lr=&pb=f&q=",$pn," - <font color=green>", true));

function getCIDName($url, $pn, $delimiter_end, $allow_recurse) {

	$phone = (string) $pn;

	if(strlen($phone)!=3 && strlen($phone)!=10) {
		return "Unknown Name";
	} else {
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url.$phone);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$str = curl_exec($ch);
		curl_close($ch);

		$j = substr($str, strpos($str, $delimiter_end));
		$t = substr($str,0,strlen($str) - strlen($j));
		$f = trim(substr($t,strrpos($t,">")+1));

		if(strlen($f)==0 && $allow_recurse==true) {
			$f = getState(getCIDName("http://www.google.com/search?hl=en&q=",substr($phone,0,3),"</font></a><br><font size=-1 class=a>www.", false))." Call";
		}
		if(strlen($f)==0 || $f==" Call"){$f="Unknown Name";}
		return $f;
	}
}

function getState($data) {
	$s = explode(",", $data);
	if(strlen($s[1])==0){
		$j = strpos($data, " - ");
		if($j===false){
			return $data;
		} else {
			return substr($data,0,$j);
		}
	}else{
		return $s[1];
	}
}
?>

Add this context to the dialplan in extensions.conf. You’ll want to make sure that in your iax.conf that “context=cidname”. Replace the “incomingiax” context listed in the script below with whatever is currently set in iax.conf

[cidname]
        exten => _X.,1,Set(cidn=${CURL(http://www.yourdomain.com/get_cidname.php?pn=${CALLERIDNUM})})
        exten => _X.,n,Set(CALLERID(name)=${cidn})
        exten => _X.,n,Goto(incomingiax|${EXTEN}|1)

Odd you would post this today, I just found this script to be very helpful: Uses 3 different services to find a CID Name

mundy.org/blog/index.php?p=82

still works fine, however use the new variable:

${CALLERID(num)}: The current Caller ID number - ${CALLERIDNUM} was used in versions of Asterisk prior to 1.2.0, it was DEPRECATED in 1.2.0 and removed in 1.4.