Other way to achieve geo IP targeting for free

On previous post, I already show you how to achieve geo IP targeting using MaxMind GeoLiteCountry. Now, I’ll show you on how to use other freely available geo IP API. We will use API provided by HostIP.info.

I’ve written small PHP function to call HostIP.info API:

//ip to location lookup
function ip2location($ip) {
    $iplocurl="http://api.hostip.info/get_html.php?ip=".$ip."&position=true";
    $data=file_get_contents($iplocurl);
    $data=str_replace(array(chr(13), chr(10)), ';', $data);
    $datas=explode(';', $data);
    
    $retval=array();
    $retval['country']=trim(str_replace('Country:', '', $datas[0]));
    $retval['city']=trim(str_replace('City:', '', $datas[1]));
    $retval['latitude']=trim(str_replace('Latitude:', '', $datas[2]));
    $retval['longitude']=trim(str_replace('Longitude:', '', $datas[3]));
    return $retval;
}

Example usage:

var_dump(ip2location($_SERVER['REMOTE_ADDR']));

Example result:

array(4) {
  ["country"]=>
  string(11) "CANADA (CA)"
  ["city"]=>
  string(11) "MARKHAM, ON"
  ["latitude"]=>
  string(0) ""
  ["longitude"]=>
  string(17) "Latitude: 43.8667"
}

One notice though: it seems less accurate than MaxMind GeoLiteCountry in term of detecting visitor’s country

Leave a Comment


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