【问题标题】:How to get the Country according to a certain IP? [duplicate]如何根据某个IP获取Country? [复制]
【发布时间】:2010-09-05 06:54:36
【问题描述】:

有谁知道从给定 IP 地址检索国家/地区的简单方法,最好采用 ISO_3166-1 格式?

【问题讨论】:

  • 不要过分依赖 IP 告诉您用户所在的国家/地区:因为我在一家斯堪的纳维亚公司工作,所以即使我们的办公室在苏格兰,我也会经常看到瑞典语的广告。 ..
  • 完全正确 - 如果您考虑一下。在很多情况下,您可能看起来来自某个地方,但实际上来自其他地方。这一切都归结于网络拓扑不必确认政治或地理边界这一事实。前任VPN、无线、卫星等。

标签: ip lookup country


【解决方案1】:

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

对于服务:

对于列表:

【讨论】:

  • 我在 SO 中看到的最有用的答案之一。谢谢!
  • 您是否知道 RIR 是否还有更详细的信息,例如来自 maxmind 的 geopipcity db,可作为开放数据使用?看了,没找到
【解决方案2】:

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

他们有一个免费版本GeoLite,它不如付费版本准确,但如果你只是追求简单的东西,它可能就足够了。

【讨论】:

  • 我今天检查时,上面的 GeoLite 链接导致 404。
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    ipinfodb 为国家/地区的 IP 提供免费的数据库和 API,反之亦然。他们使用 MaxMind 的免费数据。数据每个月都会更新,它是一个很好的免费替代方案,而且准确度不错。

    【讨论】:

    • 比维护自己的数据库要好得多。刚刚在一个非常不起眼的地方(泰国乌泰他尼)进行了测试,它就在现场。很好的发现,谢谢!
    【解决方案5】:

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

    MaxMind 产品线(付费或免费)的另一个好处是您拥有数据,不会因向另一个系统调用 Web 服务而对性能造成影响。

    【讨论】:

    • 我在英国,它说我在巴西里约 :)
    【解决方案6】:

    google'sclientlocation 返回 (my example)

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

    【讨论】:

      【解决方案7】:

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

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

      【讨论】:

        【解决方案8】:

        试试这个 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];?>
        

        【讨论】:

          【解决方案9】:

          您可以为此使用我的服务http://ipinfo.io。 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
          }
          

          如果您只是在国家代码之后,您只需在 URL 中添加 /country:

          $ 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
          

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

          【讨论】:

            【解决方案10】:

            最准确的是Digital ElementsNetAcuity。它不是免费的,但您大部分时间都得到了您所支付的费用。

            【讨论】:

              【解决方案11】:

              使用来自http://www.mmtutorialvault.com/php-ip-to-country-function/的函数ipToCountry($ip)

              【讨论】:

                【解决方案12】:

                您可以使用执行此工作的 Web 服务 API,例如:

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

                【讨论】:

                  【解决方案13】:

                  您可以免费试用IP2Location LITE database

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

                  【讨论】:

                    【解决方案14】:

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

                    API 非常快,有 10 个全局端点,每个端点每天能够处理超过 8 亿次调用。

                    这是一个 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
                        }
                    }⏎  
                    

                    【讨论】:

                      【解决方案15】:

                      您可以尝试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"
                      }
                      

                      【讨论】:

                        猜你喜欢
                        • 1970-01-01
                        • 2011-12-15
                        • 2019-05-14
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 2019-07-22
                        • 2011-10-28
                        • 1970-01-01
                        相关资源
                        最近更新 更多