I'm wondering has anyone had any experience in doing a whois on an IP and extracting the country code from that IP? Wondering what api would be the cleanest way of doing this with php.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Have a look at the MaxMind GeoIP database. They have a PHP API.
回答2:
You can use my service, the http://ipinfo.io API for this:
Assuming you want all of the details, you can use PHP's json_decode to parse the response:
function ip_details($ip) {
$json = file_get_contents("http://ipinfo.io/{$ip}");
$details = json_decode($json);
return $details;
}
$details = ip_details("8.8.8.8");
echo $details->city; // => Mountain View
echo $details->country; // => US
echo $details->org; // => AS15169 Google Inc.
echo $details->hostname; // => google-public-dns-a.google.com
JSONP is also supported, so you can call the API from javascript:
$.get("http://ipinfo.io", function(response) {
console.log(response.city);
}, "jsonp");
More details are available at http://ipinfo.io/developers