如何根据某个IP获得国家?

如何根据某个IP获得国家?,ip,lookup,country,Ip,Lookup,Country,有人知道从给定的IP地址(最好是ISO_3166-1格式)检索国家的简单方法吗?这里有一个很好的免费公共API服务: 很多人(包括我的公司)似乎都在使用MaxMind GeoIP 他们的免费版不如付费版准确,但如果你只是想买些简单的东西,那就足够了。我不知道这个网站有多准确。我刚刚访问了那个网站,它报道说我的国家是加拿大。我在美国,我办公室使用的ISP只在美国运营。它确实允许您更正猜测,但如果您使用此服务按国家跟踪网站访问者,您将无法知道数据是否正确。当然,我只是一个数据点。我下载了GeoLit

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

这里有一个很好的免费公共API服务: 很多人(包括我的公司)似乎都在使用MaxMind GeoIP

他们的免费版不如付费版准确,但如果你只是想买些简单的东西,那就足够了。

我不知道这个网站有多准确。我刚刚访问了那个网站,它报道说我的国家是加拿大。我在美国,我办公室使用的ISP只在美国运营。它确实允许您更正猜测,但如果您使用此服务按国家跟踪网站访问者,您将无法知道数据是否正确。当然,我只是一个数据点。我下载了GeoLite国家数据库,它只是一个.csv文件,我的IP地址被正确识别为我们


MaxMind产品线(付费或免费)的另一个好处是,您拥有数据,不会因调用其他系统的web服务而导致性能下降。

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

服务:

  • (如所述)
有关列表:

  • (如所述)
  • 您可以通过从RIRs下载列表来推出自己的产品:

    • 格式已记录在案

    • 最准确的是NetCuity。它不是免费的,但大部分时间你都可以得到你所付的。

      提供了一个免费的数据库和API,用于IP到国家/地区,反之亦然。他们使用MaxMind提供的免费数据。数据每月更新一次,这是一个非常好的免费选择,具有相当的准确性。

      clientlocation返回()


      您可以使用为提供的解决方案

      但是它返回一个两位数的国家代码。

      试试这个php代码

        <?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];?>
      
      
      
      使用

      中的函数ipToCountry($ip),您可以使用web服务API,其工作方式如下:

      see example of service: http://ip-api.com and usage: http://whatmyip.info
      
      你可以用我的服务,为了这个。API返回了一大堆关于IP地址的不同细节:

      $ 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
      }
      
      如果您只需要输入国家代码,只需将/country添加到URL:

      $ curl ipinfo.io/8.8.8.8/country
      US
      
      以下是您可以使用的通用PHP函数:

      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
      
      我在这些示例中使用了IP8.8.8.8,但是如果您想了解用户IP的详细信息,只需传入
      $\u服务器['REMOTE\u ADDR']
      。有关更多详细信息,请参见,其中提供了来自IP地址的多个数据点

      该API相当快,有10个全局端点,每个端点每天能够处理>800M的调用

      下面是一个卷曲示例:

      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
          }
      }⏎  
      
      你可以试试免费的

      在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;
      
      查询MySQL的PHP代码

      <?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);
       }
      }
      ?>
      
      
      
      您可以尝试一下,这是我构建的一个新的地理定位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"
      }
      

      总的来说,这是我在SO中看到的最有用的答案之一。非常感谢。你可能知道RIR是否也有更详细的信息,比如maxmind的geopipcity db,作为开放数据提供?我确实看了,但找不到。不要太依赖IP,告诉你用户所在的国家:因为我在一家斯堪的纳维亚公司工作,所以我一直看到瑞典语广告,即使我们的办公室在苏格兰……没错——如果你想一想的话。在很多情况下,你可能看起来来自某个地方,但实际上来自其他地方。这一切都归结于这样一个事实,即网络拓扑结构不必与政治或地质边界相一致。F.ex。VPN、无线、卫星等等。比维护自己的数据库要好得多。刚刚在一个非常不起眼的地方(泰国乌泰萨尼)进行了测试,结果非常准确。很好的发现,谢谢!我在英国,它说我在巴西里约:)我今天检查时,上面的GeoLite链接结果是404。
      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"
      }