<?
/* class that grabs IP info based on a couple of IP's
using the IPInfo DB http://ipinfodb.com/
*/
class IPInfo {
function getInfo($ips) {
// expects either 1 IP or an array of ips.
if (!$ips) {
return false;
}
if (!is_array($ips)) {
$ips = array($ips);
}
//IP Info db allows multiple IP request in series of up to 25. so if there are more than one, hit the
if (count($ips)>1) {
$ips = array_map(array(&$this, "cleanIP"), $ips);
$ips = array_unique($ips);
$ip_chunks = array_chunk($ips,25);
$ipinfo = array();
foreach($ip_chunks as $ipch) {
$url = "http://ipinfodb.com/ip_query2.php?timezone=false&output=json&ip=".implode(',',$ipch);
$val = $this->makeRequest($url);
foreach($val['page']['Locations'] as $i) {
$ipinfo[$i['Ip']] = $i;
}
}
return $ipinfo;
}
else {
$url = "http://ipinfodb.com/ip_query.php?timezone=false&output=json&ip=".$this->cleanIP($ips[0]);
$val = $this->makeRequest($url);
$ipinfo = array("{$ips[0]}" => $val['page']);
return array("{$ips[0]}" => $val['page']);
}
}
function cleanIP($ip) {
return preg_replace('/[^0-9.]/','',$ip);
}
private function makeRequest($url) {
$referer = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
curl_setopt($ch, CURLOPT_REFERER, $referer);
$tries=0;
$page=false;
while( $page===false && $tries<= 5) {
$page = curl_exec($ch);
$tries++;
}
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return array('page'=>json_decode($page,true), 'tries'=>$tries, 'httpcode' => $httpcode);
}
}
?>