Accessing IPInfoDB with a PHP class

I am working on an analytics tool and needed to pull info from the public IPInfoDb. You can get data for 1 – 25 IP’s at a time, and the data is returned as XML or JSON. I wrote a quick Library for PHP that accesses their data using the JSON format. It selects the correct query URL based on how many IP’s you give it.

Installation and Usage

First install the script at some PHP-accessible place, for example, /path/to/class/IPInfo.php. I would also set up autoloading classes to make things simpler. Here’s an example of pulling some information on either an individual IP or several. The class returns an associative array with IP’s for the keys and information arrays (containing city, region, country, latitude, longitude) as the value.

PHP
1
2
3
4
5
6
7
8
9
10
11
// include the class or use autoload instead.
require('/path/to/class/IPInfo.php');
// instantiate the class
$ipinfo = new IPInfo();
// option1
$oneIP = $ipinfo->getInfo('123.234.345.123');
// option2
$multipleIPs = $ipinfo->getInfo( array('123.234.345.123', '1.4.3.123') );

Download

Download the IPInfo class or copy / paste from below.

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?
/* 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);
}
}
?>

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>