【问题标题】:Getting the location from an IP address [closed]从IP地址获取位置[关闭]
【发布时间】:2010-09-29 10:16:43
【问题描述】:

我想从访问者的 IP 地址中检索他们的城市、州和国家/地区等信息,以便我可以根据他们的位置自定义我的网页。在 PHP 中有没有好的可靠的方法来做到这一点?我使用 JavaScript 编写客户端脚本,使用 PHP 编写服务器端脚本,使用 MySQL 编写数据库。

【问题讨论】:

    标签: php geolocation ip geoip


    【解决方案1】:

    您需要使用外部服务...例如http://www.hostip.info/,如果您在 google 搜索“geo-ip”可以获得更多结果。

    Host-IP API 基于 HTTP,因此您可以根据需要在 PHP 或 JavaScript 中使用它。

    【讨论】:

    • 我已经使用 hostip.info 一年了,但我没有留下深刻的印象。它通常返回未知的 9/10 检查
    • 同意,它告诉我我住在沃金……公平地说,它得到了 GB 的正确性……但我在伦敦。
    • 请记住,任何服务都只会与可用的公共信息/数据一样好。由于隐私原因,此信息只能“非常好”。如果它让您的国家/地区正确,并且至少接近州/省,我会认为这是一个胜利。如果您碰巧使用它来查找 DOS 攻击的信息,请记住 IP 甚至可能无效(未分配给任何人或超出有效的公共 IP 范围)。
    • 在他们的网站上我得到“位置:......实际上我们没有任何线索。”。我需要知道的一切
    【解决方案2】:

    查看 hostip.info 中的 API - 它提供了大量信息。
    PHP 中的示例:

    $data = file_get_contents("http://api.hostip.info/country.php?ip=12.215.42.19");
    //$data contains: "US"
    
    $data = file_get_contents("http://api.hostip.info/?ip=12.215.42.19");
    //$data contains: XML with country, lat, long, city, etc...
    

    如果您信任 hostip.info,它似乎是一个非常有用的 API。

    【讨论】:

      【解决方案3】:

      假设您想自己做而不是依赖其他提供商,IP2Nation 提供了一个 MySQL 数据库,该数据库会随着地区注册机构的变化而更新。

      【讨论】:

      • 我正在使用外部服务,突然我的网站变得非常缓慢。外部服务需要 20 秒才能发回信息——这对访问者来说显然是灾难性的。所以决定自己做,这是一个完美的解决方案。非常感谢。
      【解决方案4】:

      我喜欢 Maxmind 的免费 GeoLite City,它适用于大多数人 应用程序,如果不够精确,您可以从中升级到付费版本。包括PHP API,以及其他语言。如果您将 Lighttpd 作为网络服务器运行,您甚至可以使用 module 为每个访问者获取 SERVER 变量中的信息(如果这是您需要的)。

      我应该补充一下,还有一个免费的Geolite Country(如果您不需要查明 IP 来自哪个城市,这会更快)和 Geolite ASN(如果您想知道谁拥有该 IP)以及最后,所有这些都可以在您自己的服务器上下载,每个月都会更新,并且可以非常快速地使用提供的 API 进行查找,因为它们声明“每秒可进行数千次查找”。

      【讨论】:

      • 您可以考虑免费的 IP2Location LITE lite.ip2location.com。它具有邮政编码信息,非常有用。
      【解决方案5】:

      PHP 有一个extension for that.

      来自 PHP.net:

      GeoIP 扩展允许您查找 IP 地址的位置。 城市、州、国家、经度、纬度和其他信息 所有,例如 ISP 和连接类型都可以在 地理IP。

      例如:

      $record = geoip_record_by_name($ip);
      echo $record['city'];
      

      【讨论】:

      • 这只是 MaxMind 数据库的一个 php 模块,它是付费软件。 (免费的“精简版”版本非常不准确。)并不是说这是一件坏事,但它实际上只对商业目的有用。
      【解决方案6】:

      使用 Google API:

      <script type="text/javascript" src="http://www.google.com/jsapi"></script>
      <script>
      contry_code = google.loader.ClientLocation.address.country_code
      city = google.loader.ClientLocation.address.city
      region = google.loader.ClientLocation.address.region
      </script>
      

      【讨论】:

      • The geolocation functionality in the Loader hasn't been retired, per se. We stopped documenting it several years ago and have recommended the HTML-based solutions due to their improved accuracy, but the functionality itself has not been removed from the Loader at this time. code.google.com/p/google-ajax-apis/issues/detail?id=586
      • 这个 Google APIS IP 地理位置的质量怎么样? (是,还是我错了?),与ipinfo.io等其他服务相比?
      • 我询问了不同城市的 30 个人,Google APIS IP 地理定位质量比 ipinfo.io 差
      • @Basj Google API 就像一个魅力,而 ipinfo.io 显示我在美国,而我实际上在中欧
      【解决方案7】:

      我想我会发布,因为似乎没有人提供有关此特定 API 的信息,但它返回的正是我所追求的,你可以让它以多种格式返回,json, xml and csv

       $location = file_get_contents('http://freegeoip.net/json/'.$_SERVER['REMOTE_ADDR']);
       print_r($location);
      

      这将为您提供您可能想要的所有东西:

      {
            "ip": "77.99.179.98",
            "country_code": "GB",
            "country_name": "United Kingdom",
            "region_code": "H9",
            "region_name": "London, City of",
            "city": "London",
            "zipcode": "",
            "latitude": 51.5142,
            "longitude": -0.0931,
            "metro_code": "",
            "areacode": ""
      
      }
      

      【讨论】:

      • 嘿 Jamie FreegeoIp.net 不工作。有没有其他办法?
      • 大部分情况下,很少给出任何地区或城市
      • Tony 在 Azure 的服务似乎运行迅速且可靠:freegeoip2
      • 很快就会被弃用
      • 很遗憾,此服务将于 2018 年 7 月 1 日断开,因为此消息:“此 API 端点已弃用,将于 2018 年 7 月 1 日停止工作。有关更多信息,请访问:github.com/apilayer/freegeoip#readme\
      【解决方案8】:

      您可以下载免费的 GeoIP 数据库并在本地查找 IP 地址,或者您可以使用第三方服务并执行远程查找。这是更简单的选项,因为它不需要设置,但它确实会引入额外的延迟。

      您可以使用的第三方服务是我的http://ipinfo.io。它们提供主机名、地理位置、网络所有者和其他信息,例如:

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

      这是一个 PHP 示例:

      $ip = $_SERVER['REMOTE_ADDR'];
      $details = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));
      echo $details->city; // -> "Mountain View"
      

      您也可以在客户端使用它。这是一个简单的 jQuery 示例:

      $.get("https://ipinfo.io/json", function (response) {
          $("#ip").html("IP: " + response.ip);
          $("#address").html("Location: " + response.city + ", " + response.region);
          $("#details").html(JSON.stringify(response, null, 4));
      }, "jsonp");
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
      <h3>Client side IP geolocation using <a href="http://ipinfo.io">ipinfo.io</a></h3>
      
      <hr/>
      <div id="ip"></div>
      <div id="address"></div>
      <hr/>Full response: <pre id="details"></pre>

      【讨论】:

      • 您可能需要确保在 URL 的末尾添加一个 json,以确保您得到的是 json 而不是完整的网页。 $details = json_decode(file_get_contents("ipinfo.io{$ip}/json"));
      • 似乎不是一个非常可靠的服务。我的城市名称为null。它不像一个小城市或什么的,但它仍然无法找到它。如果它在美国不能很好地工作,我怀疑它在美国以外的返回 null 的频率。
      • 结果不准确..地区、城市邮政
      • 警告:截至 2019 年 1 月,有人报告说,由于木马,此 ipinfo 网址在 virustotal 上是 unsafe。投票时间在 2015 年到 2018 年之间
      • 它是通过此更改错误添加的 github.com/easylist/easylist/commit/… - 我们正在努力将其删除。不过,ipinfo.io 并没有什么不安全的地方。
      【解决方案9】:

      以下是我发现的一个 sn-p 的修改版本,它使用http://ipinfodb.com/ip_locator.php 来获取其信息。请记住,您也可以向他们申请 API 密钥,并直接使用 API 来获取您认为合适的信息。

      片段

      function detect_location($ip=NULL, $asArray=FALSE) {
          if (empty($ip)) {
              if (!empty($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; }
              elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; }
              else { $ip = $_SERVER['REMOTE_ADDR']; }
          }
          elseif (!is_string($ip) || strlen($ip) < 1 || $ip == '127.0.0.1' || $ip == 'localhost') {
              $ip = '8.8.8.8';
          }
      
          $url = 'http://ipinfodb.com/ip_locator.php?ip=' . urlencode($ip);
          $i = 0; $content; $curl_info;
      
          while (empty($content) && $i < 5) {
              $ch = curl_init();
              $curl_opt = array(
                  CURLOPT_FOLLOWLOCATION => 1,
                  CURLOPT_HEADER => 0,
                  CURLOPT_RETURNTRANSFER  => 1,
                  CURLOPT_URL => $url,
                  CURLOPT_TIMEOUT => 1,
                  CURLOPT_REFERER => 'http://' . $_SERVER['HTTP_HOST'],
              );
              if (isset($_SERVER['HTTP_USER_AGENT'])) $curl_opt[CURLOPT_USERAGENT] = $_SERVER['HTTP_USER_AGENT'];
              curl_setopt_array($ch, $curl_opt);
              $content = curl_exec($ch);
              if (!is_null($curl_info)) $curl_info = curl_getinfo($ch);
              curl_close($ch);
          }
      
          $araResp = array();
          if (preg_match('{<li>City : ([^<]*)</li>}i', $content, $regs)) $araResp['city'] = trim($regs[1]);
          if (preg_match('{<li>State/Province : ([^<]*)</li>}i', $content, $regs)) $araResp['state'] = trim($regs[1]);
          if (preg_match('{<li>Country : ([^<]*)}i', $content, $regs)) $araResp['country'] = trim($regs[1]);
          if (preg_match('{<li>Zip or postal code : ([^<]*)</li>}i', $content, $regs)) $araResp['zip'] = trim($regs[1]);
          if (preg_match('{<li>Latitude : ([^<]*)</li>}i', $content, $regs)) $araResp['latitude'] = trim($regs[1]);
          if (preg_match('{<li>Longitude : ([^<]*)</li>}i', $content, $regs)) $araResp['longitude'] = trim($regs[1]);
          if (preg_match('{<li>Timezone : ([^<]*)</li>}i', $content, $regs)) $araResp['timezone'] = trim($regs[1]);
          if (preg_match('{<li>Hostname : ([^<]*)</li>}i', $content, $regs)) $araResp['hostname'] = trim($regs[1]);
      
          $strResp = ($araResp['city'] != '' && $araResp['state'] != '') ? ($araResp['city'] . ', ' . $araResp['state']) : 'UNKNOWN';
      
          return $asArray ? $araResp : $strResp;
      }
      

      使用

      detect_location();
      //  returns "CITY, STATE" based on user IP
      
      detect_location('xxx.xxx.xxx.xxx');
      //  returns "CITY, STATE" based on IP you provide
      
      detect_location(NULL, TRUE);    //   based on user IP
      //  returns array(8) { ["city"] => "CITY", ["state"] => "STATE", ["country"] => "US", ["zip"] => "xxxxx", ["latitude"] => "xx.xxxxxx", ["longitude"] => "-xx.xxxxxx", ["timezone"] => "-07:00", ["hostname"] => "xx-xx-xx-xx.host.name.net" }
      
      detect_location('xxx.xxx.xxx.xxx', TRUE);   //   based on IP you provide
      //  returns array(8) { ["city"] => "CITY", ["state"] => "STATE", ["country"] => "US", ["zip"] => "xxxxx", ["latitude"] => "xx.xxxxxx", ["longitude"] => "-xx.xxxxxx", ["timezone"] => "-07:00", ["hostname"] => "xx-xx-xx-xx.host.name.net" }
      

      【讨论】:

        【解决方案10】:

        您也可以使用“smart-ip”服务:

        $.getJSON("http://smart-ip.net/geoip-json?callback=?",
            function (data) {
                alert(data.countryName);
                alert(data.city);
            }
        );
        

        【讨论】:

          【解决方案11】:

          Ben Dowling 响应中的服务已更改,因此现在更简单了。要查找位置,只需执行以下操作:

          // no need to pass ip any longer; ipinfo grabs the ip of the person requesting
          $details = json_decode(file_get_contents("http://ipinfo.io/"));
          echo $details->city; // city
          

          坐标以单个字符串形式返回,例如 '31,-80',因此您只需:

          $coordinates = explode(",", $details->loc); // -> '31,-89' becomes'31','-80'
          echo $coordinates[0]; // latitude
          echo $coordinates[1]; // longitude
          

          【讨论】:

          • 你说得对,ipinfo.io 默认会返回调用者 IP 的详细信息。如果您正在运行一个 PHP 网站,那将是您服务器的 IP,而不是用户的 IP。这就是你需要传入 $_SERVER['REMOTE_ADDR'] 的原因。如果您只想要坐标,将 /loc 附加到 URL 以获取该字段会更快。
          【解决方案12】:

          我已经对 IP 地址服务进行了大量测试,以下是我自己做的一些方法。 首先是一些我使用的有用网站的链接:

          https://db-ip.com/db 有免费的 ip-lookup 服务和一些免费的 csv 文件,你可以 下载。这使用附加到您的电子邮件的免费 api 密钥。它限制为每天 2000 个查询。

          http://ipinfo.io/ 无需 api-key 的免费 ip-lookup 服务 PHP函数:

          //uses http://ipinfo.io/.
          function ip_visitor_country($ip){
              $ip_data_in = get_web_page("http://ipinfo.io/".$ip."/json"); //add the ip to the url and retrieve the json data
              $ip_data = json_decode($ip_data_in['content'],true); //json_decode it for php use
          
              //this ip-lookup service returns 404 if the ip is invalid/not found so return false if this is the case.
              if(empty($ip_data) || $ip_data_in['httpcode'] == 404){
                  return false;
              }else{
                  return $ip_data; 
              }
          }
          
          function get_web_page($url){
              $user_agent = 'Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0';
          
              $options = array(
                  CURLOPT_CUSTOMREQUEST  =>"GET",        //set request type post or get
                  CURLOPT_POST           =>false,        //set to GET
                  CURLOPT_USERAGENT      => $user_agent, //set user agent
                  CURLOPT_RETURNTRANSFER => true,     // return web page
                  CURLOPT_HEADER         => false,    // don't return headers
                  CURLOPT_FOLLOWLOCATION => true,     // follow redirects
                  CURLOPT_ENCODING       => "",       // handle all encodings
                  CURLOPT_AUTOREFERER    => true,     // set referer on redirect
                  CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
                  CURLOPT_TIMEOUT        => 120,      // timeout on response
                  CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
              );
              $ch = curl_init( $url );
              curl_setopt_array( $ch, $options );
              $content = curl_exec( $ch );
              $err     = curl_errno( $ch );
              $errmsg  = curl_error( $ch );
              $header  = curl_getinfo( $ch );
              $httpCode = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
              curl_close( $ch );  
              $header['errno']   = $err; //curl error code
              $header['errmsg']  = $errmsg; //curl error message
              $header['content'] = $content; //the webpage result (In this case the ip data in json array form)
              $header['httpcode'] = $httpCode; //the webpage response code
              return $header; //return the collected data and response codes
          }
          

          最后你会得到这样的结果:

          Array
          (
              [ip] => 1.1.1.1
              [hostname] => No Hostname
              [city] => 
              [country] => AU
              [loc] => -27.0000,133.0000
              [org] => AS15169 Google Inc.
          )
          

          http://www.geoplugin.com/ 稍旧,但此服务为您提供了许多额外有用的信息,例如国家/地区以外的货币、大陆代码、经度等。


          http://lite.ip2location.com/database-ip-country-region-city-latitude-longitude 提供大量可下载文件,并附有将它们导入数据库的说明。 一旦您在数据库中拥有这些文件之一,您就可以相当轻松地选择数据。

          SELECT * FROM `ip2location_db5` WHERE IP > ip_from AND IP < ip_to
          

          使用php函数ip2long();将 IP 地址转换为数值。例如 1.1.1.1 变为 16843009。这使您可以扫描数据库文件给您的 ip 范围。

          所以为了找出 1.1.1.1 属于哪里,我们只需运行以下查询:

          SELECT * FROM `ip2location_db5` WHERE 16843009 > ip_from AND 16843009 < ip_to;
          

          这会返回此数据作为示例。

          FROM: 16843008
          TO: 16843263
          Country code: AU
          Country: Australia
          Region: Queensland
          City: Brisbane
          Latitude: -27.46794
          Longitude: 153.02809
          

          【讨论】:

            【解决方案13】:

            我使用来自ipapi.co 的 API 编写了一个机器人,以下是如何在php 中获取 IP 地址(例如1.2.3.4)的位置:

            设置标题:

            $opts = array('http'=>array('method'=>"GET", 'header'=>"User-Agent: mybot.v0.7.1"));
            $context = stream_context_create($opts);
            

            获取 JSON 响应

            echo file_get_contents('https://ipapi.co/1.2.3.4/json/', false, $context);
            

            获取特定字段(国家、时区等)

            echo file_get_contents('https://ipapi.co/1.2.3.4/country/', false, $context);
            

            【讨论】:

              【解决方案14】:

              这个问题是受保护的,我理解。但是,我在这里没有看到答案,我看到的是很多人展示了他们从同样的问题中得出的结论。

              目前有五个具有不同程度功能的区域互联网注册管理机构,它们是知识产权所有权方面的第一联络点。这个过程是不断变化的,这就是为什么这里的各种服务有时工作而有时不工作的原因。

              Who Is(显然)是一个古老的 TCP 协议,但它最初的工作方式是连接到端口 43,这使得通过租用连接、防火墙等进行路由时遇到问题。

              目前——大多数 Who Is 是通过 RESTful HTTP 和 ARIN 完成的,RIPE 和 APNIC 都有 RESTful 服务可以工作。 LACNIC 返回 503 而 AfriNIC 显然没有这样的 API。 (不过,它们都有在线服务。)

              这将为您提供 - IP 注册所有者的地址,但 - 不是您客户的位置 - 您必须从他们那里获得 - 您还必须要求提供。此外,在验证您认为是发起者的 IP 时,代理是您最不必担心的问题。

              人们不喜欢他们被跟踪的想法,所以——我的想法是——直接从你的客户那里得到他们的许可,并期望很多人对这个想法犹豫不决。

              【讨论】:

                【解决方案15】:

                纯 Javascript 示例,使用 https://geolocation-db.com 的服务,它们提供 JSON 和 JSONP 回调解决方案。

                不需要 jQuery!

                <!DOCTYPE html>
                <html>
                <head>
                <title>Geo City Locator by geolocation-db.com</title>
                </head>
                <body>
                    <div>Country: <span id="country"></span></div>
                    <div>State: <span id="state"></span></div>
                    <div>City: <span id="city"></span></div>
                    <div>Postal: <span id="postal"></span></div>
                    <div>Latitude: <span id="latitude"></span></div>
                    <div>Longitude: <span id="longitude"></span></div>
                    <div>IP address: <span id="ipv4"></span></div>                             
                </body>
                <script>
                
                    var country = document.getElementById('country');
                    var state = document.getElementById('state');
                    var city = document.getElementById('city');
                    var postal = document.getElementById('postal');
                    var latitude = document.getElementById('latitude');
                    var longitude = document.getElementById('longitude');
                    var ip = document.getElementById('ipv4');
                
                    function callback(data)
                    {
                        country.innerHTML = data.country_name;
                        state.innerHTML = data.state;
                        city.innerHTML = data.city;
                        postal.innerHTML = data.postal;
                        latitude.innerHTML = data.latitude;
                        longitude.innerHTML = data.longitude;
                        ip.innerHTML = data.IPv4;
                    }
                
                    var script = document.createElement('script');
                    script.type = 'text/javascript';
                    script.src = 'https://geoilocation-db.com/json/geoip.php?jsonp=callback';
                    var h = document.getElementsByTagName('script')[0];
                    h.parentNode.insertBefore(script, h);
                
                </script> 
                </html>
                

                【讨论】:

                • https://geoilocation-db.com api的使用是否有限制?
                【解决方案16】:

                如果有人偶然发现此线程,这里有另一种解决方案。在timezoneapi.io,您可以请求一个IP 地址并获得几个对象作为回报(我已经创建了该服务)。创建它是因为我需要知道我的用户所在的时区、世界上的什么地方以及现在是什么时间。

                在 PHP 中 - 返回位置、时区和日期/时间:

                // Get IP address
                $ip_address = getenv('HTTP_CLIENT_IP') ?: getenv('HTTP_X_FORWARDED_FOR') ?: getenv('HTTP_X_FORWARDED') ?: getenv('HTTP_FORWARDED_FOR') ?: getenv('HTTP_FORWARDED') ?: getenv('REMOTE_ADDR');
                
                // Get JSON object
                $jsondata = file_get_contents("http://timezoneapi.io/api/ip/?" . $ip_address);
                
                // Decode
                $data = json_decode($jsondata, true);
                
                // Request OK?
                if($data['meta']['code'] == '200'){
                
                    // Example: Get the city parameter
                    echo "City: " . $data['data']['city'] . "<br>";
                
                    // Example: Get the users time
                    echo "Time: " . $data['data']['datetime']['date_time_txt'] . "<br>";
                
                }
                

                使用 jQuery:

                // Get JSON object
                $.getJSON('https://timezoneapi.io/api/ip', function(data){
                
                    // Request OK?
                    if(data.meta.code == '200'){
                
                        // Log
                        console.log(data);
                
                        // Example: Get the city parameter
                        var city = data.data.city;
                        alert(city);
                
                        // Example: Get the users time
                        var time = data.data.datetime.date_time_txt;
                        alert(time);
                
                    }
                
                });
                

                【讨论】:

                  【解决方案17】:

                  如果您需要从 IP 地址获取位置,您可以使用可靠的地理 ip 服务,您可以获取更多详细信息here。它支持 IPv6。

                  作为奖励,它允许检查 ip 地址是否是 tor 节点、公共代理或垃圾邮件发送者。

                  您可以使用如下的javascript或php。

                  Javascript 代码:

                  $(document).ready(function () {
                          $('#btnGetIpDetail').click(function () {
                              if ($('#txtIP').val() == '') {
                                  alert('IP address is reqired');
                                  return false;
                              }
                              $.getJSON("http://ip-api.io/json/" + $('#txtIP').val(),
                                   function (result) {
                                       alert('City Name: ' + result.city)
                                       console.log(result);
                                   });
                          });
                      });
                  

                  PHP 代码:

                  $result = json_decode(file_get_contents('http://ip-api.io/json/64.30.228.118'));
                  var_dump($result);
                  

                  输出:

                  {
                  "ip": "64.30.228.118",
                  "country_code": "US",
                  "country_name": "United States",
                  "region_code": "FL",
                  "region_name": "Florida",
                  "city": "Fort Lauderdale",
                  "zip_code": "33309",
                  "time_zone": "America/New_York",
                  "latitude": 26.1882,
                  "longitude": -80.1711,
                  "metro_code": 528,
                  "suspicious_factors": {
                  "is_proxy": false,
                  "is_tor_node": false,
                  "is_spam": false,
                  "is_suspicious": false
                  }
                  

                  【讨论】:

                    【解决方案18】:

                    Ipdata.co 是一种快速、高度可用的 IP 地理定位 API,具有可靠的性能。

                    它具有极强的可扩展性,全球有 10 个端点,每个端点每秒可以处理超过 10,000 个请求!

                    此答案使用非常有限的“测试”API 密钥,仅用于测试少数调用。 Signup 获取您自己的免费 API 密钥,每天最多可收到 1500 个开发请求。

                    在php中

                    php > $ip = '8.8.8.8';
                    php > $details = json_decode(file_get_contents("https://api.ipdata.co/{$ip}?api-key=test"));
                    php > echo $details->region;
                    California
                    php > echo $details->city;
                    Mountain View
                    php > echo $details->country_name;
                    United States
                    php > echo $details->latitude;
                    37.751
                    

                    这是一个客户端示例,展示了如何获取国家、地区和城市;

                    $.get("https://api.ipdata.co?api-key=test", function (response) {
                    	$("#response").html(JSON.stringify(response, null, 4));
                      $("#country").html('Country: ' + response.country_name);
                      $("#region").html('Region ' + response.region);
                      $("#city").html('City' + response.city);  
                    }, "jsonp");
                    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                    <div id="country"></div>
                    <div id="region"></div>
                    <div id="city"></div>
                    <pre id="response"></pre>

                    免责声明;

                    我构建了服务。

                    有关多种语言的示例,请参阅Docs

                    另请参阅this 对最佳 IP 地理定位 API 的详细分析。

                    【讨论】:

                      【解决方案19】:

                      我在IPLocate.io 运行该服务,您可以通过一个简单的电话免费连接:

                      <?php
                      $res = file_get_contents('https://www.iplocate.io/api/lookup/8.8.8.8');
                      $res = json_decode($res);
                      
                      echo $res->country; // United States
                      echo $res->continent; // North America
                      echo $res->latitude; // 37.751
                      echo $res->longitude; // -97.822
                      
                      var_dump($res);
                      

                      $res 对象将包含您的地理位置字段,例如 countrycity 等。

                      查看docs了解更多信息。

                      【讨论】:

                        【解决方案20】:

                        执行 IP 地理定位有两种广泛的方法:一种是下载数据集,将其托管在您的基础架构上并保持最新状态。这需要时间和精力,尤其是当您需要支持大量请求时。另一种解决方案是使用现有的 API 服务来为您管理所有工作以及更多。

                        存在许多 API Geolocation 服务:Maxmind、Ip2location、Ipstack、IpInfo 等。最近,我工作的公司已切换到 Ipregistry (https://ipregistry.co),我参与了决策和实施过程。以下是您在寻找 IP 地理定位 API 时应考虑的一些因素:

                        • 服务准确吗?他们是否使用单一信息来源?
                        • 他们真的能承受你的负担吗?
                        • 它们是否在全球范围内提供一致且快速的响应时间(除非您的用户是特定国家/地区的)?
                        • 他们的定价模式是什么?

                        以下是获取 IP 地理位置信息的示例(但也可以通过一次调用获取威胁和用户代理数据):

                        $ip = $_SERVER['REMOTE_ADDR'];
                        $details = json_decode(file_get_contents("https://api.ipregistry.co/{$ip}?key=tryout"));
                        echo $details->location;
                        

                        注意:我不是来推广 Ipregistry 并说它是最好的,但我花了很长时间分析现有的解决方案,他们的解决方案确实很有希望。

                        【讨论】:

                          猜你喜欢
                          • 2014-02-13
                          • 2013-11-09
                          • 2018-03-30
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          相关资源
                          最近更新 更多