Good php API for extracting country code from IP? [closed]

半世苍凉 提交于 2020-01-02 19:25:08

问题


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



来源:https://stackoverflow.com/questions/4904235/good-php-api-for-extracting-country-code-from-ip

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!