How to get the Country according to a certain IP?

2019-01-03 22:21发布

Does anyone know of a simple way to retrieve the country for a given IP Address? Preferably in ISO_3166-1 format?

14条回答
啃猪蹄的小仙女
2楼-- · 2019-01-03 23:10

The most accurate is Digital Elements NetAcuity...not free but you get what you pay for most of the time....Digital Element

查看更多
来,给爷笑一个
3楼-- · 2019-01-03 23:11

A lot of people (including my company) seem to use MaxMind GeoIP.

They have a free version GeoLite which is not as accurate as the paid version, but if you're just after something simple, it may be good enough.

查看更多
甜甜的少女心
4楼-- · 2019-01-03 23:16

You can use my service, http://ipinfo.io, for this. The API returns a whole bunch of different details about an IP address:

$ curl ipinfo.io/8.8.8.8
{
  "ip": "8.8.8.8",
  "hostname": "google-public-dns-a.google.com",
  "loc": "37.385999999999996,-122.0838",
  "org": "AS15169 Google Inc.",
  "city": "Mountain View",
  "region": "CA",
  "country": "US",
  "phone": 650
}

If you're only after the country code you just need to add /country to the URL:

$ curl ipinfo.io/8.8.8.8/country
US

Here's a generic PHP function you could use:

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

I've used the IP 8.8.8.8 in these examples, but if you want details for the user's IP just pass in $_SERVER['REMOTE_ADDR'] instead. More details are available at http://ipinfo.io/developers

查看更多
倾城 Initia
5楼-- · 2019-01-03 23:18

There are two approaches: using an Internet service and using some kind of local list (perhaps wrapped in a library). What you want will depend on what you are building.

For services:

For lists:

查看更多
孤傲高冷的网名
6楼-- · 2019-01-03 23:18

See ipdata.co which gives you several data points from an ip address.

The API is pretty fast, with 10 global endpoints each able to handle >800M calls daily.

Here's a curl example;

curl https://api.ipdata.co/78.8.53.5
{
    "ip": "78.8.53.5",
    "city": "G\u0142og\u00f3w",
    "region": "Lower Silesia",
    "region_code": "DS",
    "country_name": "Poland",
    "country_code": "PL",
    "continent_name": "Europe",
    "continent_code": "EU",
    "latitude": 51.6461,
    "longitude": 16.1678,
    "asn": "AS12741",
    "organisation": "Netia SA",
    "postal": "67-200",
    "currency": "PLN",
    "currency_symbol": "z\u0142",
    "calling_code": "48",
    "flag": "https://ipdata.co/flags/pl.png",
    "emoji_flag": "\ud83c\uddf5\ud83c\uddf1",
    "time_zone": "Europe/Warsaw",
    "is_eu": true,
    "suspicious_factors": {
        "is_tor": false
    }
}⏎  
查看更多
可以哭但决不认输i
7楼-- · 2019-01-03 23:20

ipinfodb provides a free database and API for IP to country and vice versa. They use free data from MaxMind. The data gets updated every month, and it's a great free alternative with decent accuracy.

查看更多
登录 后发表回答