1

In my PHP based application i would like to capture the location of user machine when user logs in to the webapp based on IP address.

I followed this method:

$url = "http://freegeoip.net/json/$ip";
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$data = curl_exec($ch);
curl_close($ch);

i am getting the latitude and longitude values but these are not exact, instead they are absolute. In the response i am getting JSON like this:

{
["ip"]=> string(11) "59.90.210.9" 
["country_code"]=> string(2) "IN" 
["country_name"]=> string(5) "India" 
["region_code"]=> string(0) "" 
["region_name"]=> string(0) "" 
["city"]=> string(0) "" 
["zip_code"]=> string(0) "" 
["time_zone"]=> string(12) "Asia/Kolkata" 
["latitude"]=> int(20) 
["longitude"]=> int(77) 
["metro_code"]=> int(0) 
} 

No city no zip nothing. Is there any better API to get exact things as required? Thanks in advance for any response.

  • 2
    IP will *never* give you an exact location, your lucky if the city is correct –  Jun 20 '15 at 06:24

3 Answers3

1

There is a whole lot of other services. You might want to look at this: Longitude and latitude value from IP address

Community
  • 1
  • 1
Amitabh Kant
  • 145
  • 2
  • 6
0

You can use MaxMind's GeoIP service / database which has two versions: GeoIP2 and (Legacy) GeoIP. They provide free databases and commercial databases, the latter of which is more accurate. They have a GeoIP2 PHP SDK on Github and Legacy GeoIP is included with PHP.

The MaxMind GeoIP2 Demo Page allows you to input an IP address and get back the following data:

  1. Coordinates (lat/lon)
  2. Country Code (2 letter country code)
  3. Domain
  4. ISP
  5. Location (city, state, country, continent)
  6. Metro Code
  7. Organization
  8. Postal Code (e.g. zip code)

Links:

  1. MaxMind's GeoIP2 and Legacy GeoIP product page
  2. GeoIP2 demo page (enter IPs and get results)
  3. GeoIP2-php SDK on Github
  4. GeoIP on PHP.net
Grokify
  • 15,092
  • 6
  • 60
  • 81
0

Ricky, you can use IP2Location Geolocation service or database. It comes with two versions: a) commercial and b) LITE. They have a developer page of programming API in many programming languages.

There is an online demo page allows you to get the following data using single IP address:

  1. Location (country, state, city)
  2. Coordinates (latitude, longitude)
  3. Elevation
  4. Postal Codes
  5. Phone (IDD and area code)
  6. Domain Name
  7. ISP Name
  8. Time Zone
  9. Connection Speed
  10. Weather Station (weather station name and code)
  11. Mobile Carrier (mnc, mcc, brand)
  12. Usage Type
Michael C.
  • 1,410
  • 1
  • 9
  • 20