Good php API for extracting country code from IP?

2019-05-23 18:21发布

问题:

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