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:
- coordinates (lat/lon)
- country code (2 letter country code)
- domain
- isp
- location (city, state, country, continent)
- metro code
- organization
- postal code (e.g. zip code)
links:
Comments
Post a Comment