关于查找:如何根据某个IP获得国家/地区?

关于查找:如何根据某个IP获得国家/地区?

How to get the Country according to a certain IP?

本问题已经有最佳答案,请猛点这里访问。

有人知道从给定IP地址(最好是ISO_3166-1格式)中检索国家/地区的任何简单方法吗?


有两种方法:使用Internet服务和使用某种本地列表(可能包装在库中)。您想要的内容取决于您要构建的内容。

对于服务:

  • http://www.hostip.info/use.html(如Mark所述)
  • http://www.team-cymru.org/Services/ip-to-asn.html

对于列表:

  • http://www.maxmind.com/app/geoip_country(如Orion所述)
  • 您可以通过从RIR下载列表来滚动自己的列表:

    • ftp.arin.net/pub/stats/arin/delegated-arin-latest
    • ftp.ripe.net/ripe/stats/delegated-ripencc-latest
    • ftp.afrinic.net/pub/stats/afrinic/delegated-afrinic-latest
    • ftp.apnic.net/pub/stats/apnic/delegated-apnic-latest
    • ftp.lacnic.net/pub/stats/lacnic/delegated-lacnic-latest

      此自述文件中记录了格式


很多人(包括我的公司)似乎都在使用MaxMind GeoIP。

他们有一个免费版本的GeoLite,它的准确性不如付费版本,但是如果您只是简单一些,那么可能就足够了。


这是一个带有公共API的不错的免费服务:
http://www.hostip.info/use.html


ipinfodb提供免费的数据库和API,用于IP到国家(反之亦然)。他们使用来自MaxMind的免费数据。数据每个月都会更新,这是一个不错的免费替代方法,而且准确性很高。


我不知道http://hostip.info网站的准确性如何。我刚刚访问了该站点,它报告说我的国家是加拿大。我在美国,而我的办公室使用的ISP仅在美国运营。它的确可以使您更正其猜测,但是,如果您使用此服务按国家/地区跟踪网站访问者,则无法知道数据是否正确。当然,我只是一个数据点。我下载了GeoLite国家数据库,它只是一个.csv文件,并且我的IP地址已正确标识为美国。

MaxMind产品系列(收费或免费)的另一个好处是,您拥有数据,不会导致对另一个系统进行Web服务调用而导致性能下降。


您可以为此使用我的服务http://ipinfo.io。 API返回有关IP地址的许多不同详细信息:

1
2
3
4
5
6
7
8
9
10
11
$ 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
}

如果仅在国家/地区代码之后,只需在URL上添加/ country:

1
2
$ curl ipinfo.io/8.8.8.8/country
US

这是您可以使用的通用PHP函数:

1
2
3
4
5
6
7
8
9
10
11
12
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

在这些示例中,我使用了IP 8.8.8.8,但是如果要获取用户IP的详细信息,则只需传入$_SERVER['REMOTE_ADDR']。有关更多详细信息,请访问http://ipinfo.io/developers


尝试此php代码

1
2
3
4
  <?php  $ip = $_SERVER['REMOTE_ADDR'];
    $json = file_get_contents("http://api.easyjquery.com/ips/?ip=".$ip."&full=true");
    $json = json_decode($json,true);
    $timezone = $json[localTimeZone];?>

您可以使用此问题提供的解决方案。

但是它返回一个2位数的国家/地区代码。


google的clientlocation返回(我的示例)

1
2
3
latlng = new google.maps.LatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude);
location ="IP location:" + getFormattedLocation();
document.getElementById("location").innerHTML = location;

最准确的是Digital Elements NetAcuity。它不是免费的,但您大部分时间都能获得所需要的费用。


您可以使用Web服务API,其工作方式如下:

1
see example of service: http://ip-api.com and usage: http://whatmyip.info

使用http://www.mmtutorialvault.com/php-ip-to-country-function/

中的ipToCountry($ ip)函数


您可以尝试免费的IP2Location LITE数据库

在MySQL中创建表

1
2
3
4
5
6
7
8
9
CREATE DATABASE ip2location;
USE ip2location;
CREATE TABLE `ip2location_db1`(
    `ip_from` INT(10) UNSIGNED,
    `ip_to` INT(10) UNSIGNED,
    `country_code` CHAR(2),
    `country_name` VARCHAR(64),
    INDEX `idx_ip_to` (`ip_to`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

要导入数据

1
2
3
4
5
6
7
8
9
10
LOAD DATA LOCAL
    INFILE 'IP2LOCATION-LITE-DB1.CSV'
INTO TABLE
    `ip2location_db1`
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\
\
'
IGNORE 0 LINES;

用于查询MySQL的PHP??代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
// Replace this MYSQL server variables with actual configuration
$mysql_server ="mysql_server.com";
$mysql_user_name ="UserName";
$mysql_user_pass ="Password";

// Retrieve visitor IP address from server variable REMOTE_ADDR
$ipaddress = $_SERVER["REMOTE_ADDR"];

// Convert IP address to IP number for querying database
$ipno = Dot2LongIP($ipaddress);

// Connect to the database server
$link = mysql_connect($mysql_server, $mysql_user_name, $mysql_user_pass) or die("Could not connect to MySQL database");

// Connect to the IP2Location database
mysql_select_db("ip2location") or die("Could not select database");

// SQL query string to match the recordset that the IP number fall between the valid range
$query ="SELECT * FROM ip2location_db1 WHERE $ipno <= ip_to LIMIT 1";

// Execute SQL query
$result = mysql_query($query) or die("IP2Location Query Failed");

// Retrieve the recordset (only one)
$row = mysql_fetch_object($result);

// Keep the country information into two different variables
$country_code = $row->country_code;
$country_name = $row->country_name;

echo"Country_code:" . $country_code ."<br/>";
echo"Country_name:" . $country_name ."<br />";

// Free recordset and close database connection
mysql_free_result($result);
mysql_close($link);

// Function to convert IP address (xxx.xxx.xxx.xxx) to IP number (0 to 256^4-1)
function Dot2LongIP ($IPaddr) {
 if ($IPaddr =="")
 {
   return 0;
 } else {
   $ips = explode(".", $IPaddr);
   return ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 * 256 * 256);
 }
}
?>

请参阅ipdata.co,它为您提供了IP地址中的多个数据点。

该API相当快,有10个全局终结点,每个终结点每天能够处理超过800M次呼叫。

这是一个卷曲的例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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":"/d/jc/2023041114/dqaotdygm2s42.webp",
   "emoji_flag":"\\ud83c\\uddf5\\ud83c\\uddf1",
   "time_zone":"Europe/Warsaw",
   "is_eu": true,
   "suspicious_factors": {
       "is_tor": false
    }
}?

您可以尝试访问https://astroip.co,这是我构建的新的Geolocation API,该API公开了地理数据以及其他有用的数据点,例如货币,时区,ASN数据和安全性。

这是json响应的示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
curl https://api.astroip.co/70.163.7.1
{
 "status_code": 200,
 "geo": {
   "is_metric": false,
   "is_eu": false,
   "longitude": -77.0924,
   "latitude": 38.7591,
   "country_geo_id": 6252001,
   "zip_code":"22306",
   "city":"Alexandria",
   "region_code":"VA",
   "region_name":"Virginia",
   "continent_code":"NA",
   "continent_name":"North America",
   "capital":"Washington",
   "country_name":"United States",
   "country_code":"US"
  },
 "asn": {
   "route":"70.160.0.0/14",
   "type":"isp",
   "domain":"cox.net",
   "organization":"ASN-CXA-ALL-CCI-22773-RDC",
   "asn":"AS22773"
  },
 "currency": {
   "native_name":"US Dollar",
   "code":"USD",
   "name":"US Dollar",
   "symbol":"$"
  },
 "timezone": {
   "is_dst": false,
   "gmt_offset": -18000,
   "date_time":"2020-12-05T17:04:48-05:00",
   "microsoft_name":"Eastern Standard Time",
   "iana_name":"America/New_York"
  },
 "security": {
   "is_crawler": false,
   "is_proxy": false,
   "is_tor": false,
   "tor_insights": null,
   "proxy_insights": null,
   "crawler_insights": null
  },
 "error": null,
 "ip_type":"ipv4",
 "ip":"70.163.7.1"
}

推荐阅读