I am having some issues with the hostip.info API
.
Here is my PHP code:
$ip = $_SERVER['REMOTE_ADDR'];
echo $ip;
$response = file('http://api.hostip.info/get_html.php?ip='.$ip);
foreach ($response as $line) {
$line = trim($line);
if (!empty($line)) {
$parts = explode(': ', $line);
$array[$parts[0]] = $parts[1];
}
}
It returns my correct IP address, but when I try returning $array['City']
it says I'm in a different state. I am not using a proxy. Why is it giving me the correct IP, but not city?
Thanks!
Your ip address belongs to your ISP and they have registered it with the address this api returns. There is no possible way for an api to determine the actual geographic location of a given ip address with 100% accuracy. For example, I'm currently in California but this service http://api.hostip.info/ responds with "Reston, VA" because I'm on Verizon and that's where Verizon registered the ip addresses.
Here is a good article about the subject:
There are more accurate services out there, such as https://www.maxmind.com/en/geoip2-precision-insights but I don't know if there are any that are free.
Edit: Just found this site that shows a nice comparison of the results from various similar services: http://www.iplocation.net/
and from that site, this explanation:
Accuracy of geolocation database varies depending on which database you use. For IP-to-country database, some vendors claim to offer 98% to 99% accuracy although typical Ip2Country database accuracy is more like 95%. For IP-to-Region (or City), accracy range anywhere from 50% to 75% if neighboring cities are treated as correct. Considering that there is no official source of IP-to-Region information, 50+% accuracy is pretty good.
I hope this answers your question.