Find GeoLocation Information of an IP Address using PHP and Python

GeoLocation of IP Address PHP Python

Demo Download

In this tutorial, we’re going to find country, region, city, latitude and longitude of an user by user’s IP Address, simply finding geolocation of IP Address using API

First register for an account here and get your API KEY for accessing API. Make sure you enter Server IP correctly while registering.

Find Country, City, Latitude and Longitude using Python

import urllib2
import json

def getIPAddress(api_key,ip_address):
    api_endpoint = "" +api_key+"&ip="+ip_address+"&format=json"
        api_response = urllib2.urlopen(api_endpoint)
            return json.loads(
        except (ValueError, KeyError, TypeError):
            return "JSON format error"
    except IOError, e:
        if hasattr(e, 'code'):
            return e.code
        elif hasattr(e, 'reason'):
            return e.reason

api_key = "YOUR_API_KEY" 
ip_address = "IP_ADDRESS"

data = getIPAddress(api_key,ip_address)
#print data
if data['statusCode'] == "OK":
    print "IP: "+ ip_address
    print "API Status:"+ data['statusCode']
    print "Country:"+ data['countryName']
    print "Region:"+ data['regionName']
    print "City:"+ data['cityName']
    print "Latitude:"+ data['latitude']
    print "Longitude:"+ data['longitude']
    print data['statusCode']
    print data['statusMessage']

Python modules used:

  • urllib2
  • json

Find Country, City, Latitude and Longitude using PHP

    $ipAddress = "IP_ADDRESS";
    $ip_key = "YOUR_API_KEY";

    $query = "" . $ip_key . "&ip=" . $ipAddress . "&format=json";
    $json = file_get_contents($query);
    $data = json_decode($json, true);

    if ($data['statusCode'] == "OK") {
        echo '<pre>';
        echo "IP Address: " . $ipAddress;
        echo "Country: " . $data['countryName'];
        echo "Region: " . $data['regionName'];
        echo "City: " . $data['cityName'];
        echo "Latitude: " . $data['latitude'];
        echo "Longitude: " . $data['longitude'];
        echo '</pre>';
    } else {
        echo $data['statusCode']." ".$data['statusMessage'];


Demo & Download

Demo Download