Match An IP Address To A Physical Location With PHP

Sometimes it’s useful to locate where a certain IP address is located. Usually this isn’t really that accurate but at least it gives you a general idea for stats or marketing research. I’ve written a simple php function that uses curl to query a IP address location database for the location info. In the function I specify that I want a JSON data structure containing the location information. I chose JSON over the XML alternative just for ease of use. The object that we get returned needs to be referenced in a specific kind of way. I show how under the function.
This function uses the IPInfoDB JSON php API. There are quite a few alternatives out there so feel free to do your research. The IPInfoDB PHP API returns :

  • Country Code (CA or US)
  • Country Name (Canada)
  • Region Code
  • Region Name (Province or Dtate)
  • City
  • Postalcode or Zip Code (which doesn’t always work)
  • Latitude
  • Longitude
  • gmt offset
  • Dst offset


function get_location_info($ip_address)
{
  $url = 'http://ipinfodb.com/ip_query.php?ip=';
  $url .= $ip_address;
        $url .= '&output=json';
  $ch = curl_init();
  
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
  $json_packet = curl_exec($ch);
  $location_array = json_decode($json_packet);
  curl_close($ch);
  return($location_array);

}//get_location_info

$loc_arr = get_location_info("69.157.107.64");
echo "\n";
echo $loc_arr->{'City'};
echo "\n";
echo $loc_arr->{'RegionName'};
echo "\n";
echo $loc_arr->{'CountryCode'};

Hopefully theres no confusion there.

Share

6 Responses to “Match An IP Address To A Physical Location With PHP”

  • GoGoGa Says:

    You should add a curl timeout or your script will hang for a log time if the server is not available… had this problem with another webservice. ;)

  • Tony Smith Says:

    A similar operation could be performed in a better way at the private search engine site http://www.aafter.com, where you just need to type the phrase IP? and the search engine returns the physical location.

    Tony Smith

  • Cody Taylor Says:

    That does work. But the whole point is to do this in php for any ip address. Not get my own IP location.

  • Cody Taylor Says:

    @GoGoGa I should definitely do that. Good advice.

  • Subhankar Ray Says:

    Cody,

    Good information. Your are right this is a programmatic way to find out the geo location from IP. However, for a quick lookup one can type the IP address at the AAfter search box, it returns location, ISP info, and if the IP is trusted.

    Subhankar Ray
    AAfter Search

  • abcphp.com Says:

    Match An IP Address To A Physical Location With PHP | tech stuff…

    Sometimes it’s useful to locate where a certain IP address is located. Usually this isn’t really that accurate but at least it gives you a general idea for stats or marketing research. I’ve written a simple php function that uses curl to query a IP add…