如何根据特定IP获取国家信息?

61

有没有简单的方法从给定的IP地址中检索国家,最好是以ISO_3166-1格式?


8
不要过度依赖IP地址来判断用户所在的国家/地区:我所在的公司是一家斯堪的纳维亚公司,但我们的办公室却在苏格兰,因此我经常看到用瑞典语写的广告。请注意,这并不代表该广告面向的是瑞典用户。 - AAT
确切地说,如果你仔细想想的话,有很多情况下你可能看起来来自某个地方,但实际上来自另一个地方。这都是因为网络拓扑结构不必遵循政治或地理边界所致。例如,VPN、无线、卫星等等。 - Phluks
15个回答

2

您可以使用Web服务API来完成此工作,例如:

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

2

1
你可以尝试免费的IP2Location LITE数据库
在MySQL中创建表:
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;

导入数据

LOAD DATA LOCAL
    INFILE 'IP2LOCATION-LITE-DB1.CSV'
INTO TABLE
    `ip2location_db1`
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 0 LINES;

PHP代码查询MySQL

<?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);
 }
}
?>

1

查看ipdata.co,可以从IP地址获取多个数据点。

该API非常快速,有10个全球端点,每个端点每天能够处理超过800M的调用。

以下是一个curl示例:

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
    }
}

-1
你可以尝试一下https://astroip.co,这是我建立的一个新的地理位置API,它提供了地理数据以及其他有用的数据点,如货币、时区、ASN数据和安全性。
以下是JSON响应示例:
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"
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接