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) {
    $data=str_replace(array(chr(13), chr(10)), ';', $data);
    $datas=explode(';', $data);
    $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:


Example result:

array(4) {
  string(11) "CANADA (CA)"
  string(11) "MARKHAM, ON"
  string(0) ""
  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>