php - How to obtain accurate latitude and longitude of user machine based on IP address -


in php based application capture location of user machine when user logs in webapp based on ip address.

i followed 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 getting latitude , longitude values these not exact, instead absolute. in response getting json 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. there better api exact things required? in advance response.

you can use maxmind's geoip service / database has 2 versions: geoip2 , (legacy) geoip. provide free databases , commercial databases, latter of more accurate. have geoip2 php sdk on github , legacy geoip included php.

the maxmind geoip2 demo page allows input ip address , 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 , legacy geoip product page
  2. geoip2 demo page (enter ips , results)
  3. geoip2-php sdk on github
  4. geoip on php.net

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -