如何使用 JavaScript 获取客户端的 IP 地址?

新手上路,请多包涵

我需要使用 JavaScript 以某种方式检索客户端的 IP 地址;没有服务器端代码,甚至没有 SSI。

但是,我并不反对使用免费的第 3 方脚本/服务。

原文由 FlySwat 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.4k
1 个回答

我会使用可以返回 JSON 的 Web 服务(与 jQuery 一起使事情变得更简单)。以下是我能找到的所有 活跃 的免费 IP 查询服务以及它们返回的信息。如果您知道其他人,请添加评论,我会更新此答案。


抽象的

 let apiKey = '1be9a6884abd4c3ea143b59ca317c6b2';
$.getJSON('https://ipgeolocation.abstractapi.com/v1/?api_key=' + apiKey, function(data) {
  console.log(JSON.stringify(data, null, 2));
});
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

限制:

  • 每月 10,000 个请求
  • 需要注册才能获取您的 API 密钥

大数据云

 // Base
let apiKey = 'd9e53816d07345139c58d0ea733e3870';
$.getJSON('https://api.bigdatacloud.net/data/ip-geolocation?key=' + apiKey, function(data) {
  console.log(JSON.stringify(data, null, 2));
});
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
 // Base + Confidence Area
let apiKey = 'd9e53816d07345139c58d0ea733e3870';
$.getJSON('https://api.bigdatacloud.net/data/ip-geolocation-with-confidence?key=' + apiKey, function(data) {
  console.log(JSON.stringify(data, null, 2));
});
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
 // Base + Confidence Area + Hazard Report
let apiKey = 'd9e53816d07345139c58d0ea733e3870';
$.getJSON('https://api.bigdatacloud.net/data/ip-geolocation-full?key=' + apiKey, function(data) {
  console.log(JSON.stringify(data, null, 2));
});
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

限制:

  • 每月 10,000 个请求
  • 需要注册才能获取您的 API 密钥

云焰

 $.get('https://www.cloudflare.com/cdn-cgi/trace', function(data) {
  // Convert key-value pairs to JSON
  // https://stackoverflow.com/a/39284735/452587
  data = data.trim().split('\n').reduce(function(obj, pair) {
    pair = pair.split('=');
    return obj[pair[0]] = pair[1], obj;
  }, {});
  console.log(data);
});
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

限制:

  • 返回纯文本
  • 如果有,则仅返回 IPv6 地址

数据库IP

试试看: https ://api.db-ip.com/v2/free/self

 $.getJSON('https://api.db-ip.com/v2/free/self', function(data) {
  console.log(JSON.stringify(data, null, 2));
});

退货:

 {
  "ipAddress": "116.12.250.1",
  "continentCode": "AS",
  "continentName": "Asia",
  "countryCode": "SG",
  "countryName": "Singapore",
  "city": "Singapore (Queenstown Estate)"
}

限制:

  • 每天 1,000 个请求
  • 要求非空 Origin 请求头

地理字节

试试看: http: //gd.geobytes.com/GetCityDetails

 $.getJSON('http://gd.geobytes.com/GetCityDetails?callback=?', function(data) {
  console.log(JSON.stringify(data, null, 2));
});

退货:

 {
  "geobytesforwarderfor": "",
  "geobytesremoteip": "116.12.250.1",
  "geobytesipaddress": "116.12.250.1",
  "geobytescertainty": "99",
  "geobytesinternet": "SA",
  "geobytescountry": "Saudi Arabia",
  "geobytesregionlocationcode": "SASH",
  "geobytesregion": "Ash Sharqiyah",
  "geobytescode": "SH",
  "geobyteslocationcode": "SASHJUBA",
  "geobytescity": "Jubail",
  "geobytescityid": "13793",
  "geobytesfqcn": "Jubail, SH, Saudi Arabia",
  "geobyteslatitude": "27.004999",
  "geobyteslongitude": "49.660999",
  "geobytescapital": "Riyadh ",
  "geobytestimezone": "+03:00",
  "geobytesnationalitysingular": "Saudi Arabian ",
  "geobytespopulation": "22757092",
  "geobytesnationalityplural": "Saudis",
  "geobytesmapreference": "Middle East ",
  "geobytescurrency": "Saudi Riyal",
  "geobytescurrencycode": "SAR",
  "geobytestitle": "Saudi Arabia"
}

限制:

  • 每小时 16,384 个请求
  • 免费计划没有 SSL (https)
  • 可以返回错误的位置

GeoIPLookup.io

 $.getJSON('https://json.geoiplookup.io/?callback=?', function(data) {
  console.log(JSON.stringify(data, null, 2));
});
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

限制:

  • 每小时 10,000 个请求
  • 仅供非商业用途的免费计划
  • 如果有,则仅返回 IPv6 地址

地理插件

试一试: http ://www.geoplugin.net/json.gp

 $.getJSON('http://www.geoplugin.net/json.gp', function(data) {
  console.log(JSON.stringify(data, null, 2));
});

退货:

 {
  "geoplugin_request": "116.12.250.1",
  "geoplugin_status": 200,
  "geoplugin_credit": "Some of the returned data includes GeoLite data created by MaxMind, available from <a href=\\'http://www.maxmind.com\\'>http://www.maxmind.com</a>.",
  "geoplugin_city": "Singapore",
  "geoplugin_region": "Singapore (general)",
  "geoplugin_areaCode": "0",
  "geoplugin_dmaCode": "0",
  "geoplugin_countryCode": "SG",
  "geoplugin_countryName": "Singapore",
  "geoplugin_continentCode": "AS",
  "geoplugin_latitude": "1.2931",
  "geoplugin_longitude": "103.855797",
  "geoplugin_regionCode": "00",
  "geoplugin_regionName": "Singapore (general)",
  "geoplugin_currencyCode": "SGD",
  "geoplugin_currencySymbol": "&#36;",
  "geoplugin_currencySymbol_UTF8": "$",
  "geoplugin_currencyConverter": 1.4239
}

限制:

  • 每分钟 120 个请求
  • 免费计划没有 SSL (https)

黑客目标

 $.get('https://api.hackertarget.com/geoip/?q=116.12.250.1', function(data) {
  // Convert key-value pairs to JSON
  // https://stackoverflow.com/a/39284735/452587
  data = data.trim().split('\n').reduce(function(obj, pair) {
    pair = pair.split(': ');
    return obj[pair[0]] = pair[1], obj;
  }, {});
  console.log(data);
});
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

限制:

  • 每天 100 个请求
  • 需要IP地址参数
  • 返回纯文本

ipapi

试试看: https ://ipapi.co/json/

 $.getJSON('https://ipapi.co/json/', function(data) {
  console.log(JSON.stringify(data, null, 2));
});

退货:

 {
  "ip": "116.12.250.1",
  "city": "Singapore",
  "region": "Central Singapore Community Development Council",
  "country": "SG",
  "country_name": "Singapore",
  "postal": null,
  "latitude": 1.2855,
  "longitude": 103.8565,
  "timezone": "Asia/Singapore"
}

限制:

  • 每天 1,000 个请求
  • 需要 SSL (https)
  • 要求非空 Origin 请求头
  • 如果有,则仅返回 IPv6 地址

IP-API

试试看:http: //ip-api.com/json

 $.getJSON('http://ip-api.com/json', function(data) {
  console.log(JSON.stringify(data, null, 2));
});

退货:

 {
  "as": "AS3758 SingNet",
  "city": "Singapore",
  "country": "Singapore",
  "countryCode": "SG",
  "isp": "SingNet Pte Ltd",
  "lat": 1.2931,
  "lon": 103.8558,
  "org": "Singapore Telecommunications",
  "query": "116.12.250.1",
  "region": "01",
  "regionName": "Central Singapore Community Development Council",
  "status": "success",
  "timezone": "Asia/Singapore",
  "zip": ""
}

限制:

  • 每分钟 150 个请求
  • 免费计划没有 SSL (https)

ip数据

 let apiKey = 'be0f755b93290b4c100445d77533d291763a417c75524e95e07819ad';
$.getJSON('https://api.ipdata.co?api-key=' + apiKey, function(data) {
  console.log(JSON.stringify(data, null, 2));
});
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

限制:

  • 每天 1,500 个请求
  • 需要注册才能获取您的 API 密钥
  • 需要 SSL (https)

IP查找

 let apiKey = '50e887ce-e3bb-4f00-a9b9-667597db5539';
$.getJSON('https://ipfind.co/me?auth=' + apiKey, function(data) {
  console.log(JSON.stringify(data, null, 2));
});
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

限制:

  • 每天 300 个请求
  • 需要注册才能获取您的 API 密钥

ip地理位置

 let apiKey = 'f8e0b361e8f4405c94613ab534959fdf';
$.getJSON('https://api.ipgeolocation.io/ipgeo?apiKey=' + apiKey, function(data) {
  console.log(JSON.stringify(data, null, 2));
});
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

限制:

  • 每月 50,000 个请求
  • 需要注册才能获取您的 API 密钥
  • 如果有,则仅返回 IPv6 地址

IP化

 $.getJSON('https://api.ipify.org?format=jsonp&callback=?', function(data) {
  console.log(JSON.stringify(data, null, 2));
});
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

限制:

  • 没有任何

IPInfoDB

 let apiKey = '25864308b6a77fd90f8bf04b3021a48c1f2fb302a676dd3809054bc1b07f5b42';
$.getJSON('https://api.ipinfodb.com/v3/ip-city/?format=json&key=' + apiKey, function(data) {
  console.log(JSON.stringify(data, null, 2));
});
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

限制:

  • 每秒两个请求
  • 需要注册才能获取您的 API 密钥

ipinfo.io

 $.getJSON('https://ipinfo.io/json', function(data) {
  console.log(JSON.stringify(data, null, 2));
});
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

限制:

  • 每月 50,000 个请求

登记处

 $.getJSON('https://api.ipregistry.co/?key=tryout', function(data) {
  console.log(JSON.stringify(data, null, 2));
});
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

限制:

  • 免费计划包括 100,000 个请求
  • 需要注册才能获取您的 API 密钥
  • 如果有,则仅返回 IPv6 地址

ipstack (以前称为 freegeoip.net)

试试看: http://api.ipstack.com/ __ ?access_key= __

 $.getJSON('http://api.ipstack.com/<ip_address>?access_key=<your_api_key>', function(data) {
  console.log(JSON.stringify(data, null, 2));
});

退货:

 {
  "ip": "116.12.250.1",
  "type": "ipv4",
  "continent_code": "AS",
  "continent_name": "Asia",
  "country_code": "SG",
  "country_name": "Singapore",
  "region_code": "01",
  "region_name": "Central Singapore Community Development Council",
  "city": "Singapore",
  "zip": null,
  "latitude": 1.2931,
  "longitude": 103.8558,
  "location": {
    "geoname_id": 1880252,
    "capital": "Singapore",
    "languages": [
      {
        "code": "en",
        "name": "English",
        "native": "English"
      },
      {
        "code": "ms",
        "name": "Malay",
        "native": "Bahasa Melayu"
      },
      {
        "code": "ta",
        "name": "Tamil",
        "native": "தமிழ்"
      },
      {
        "code": "zh",
        "name": "Chinese",
        "native": "中文"
      }
    ],
    "country_flag": "http://assets.ipstack.com/flags/sg.svg",
    "country_flag_emoji": "🇸🇬",
    "country_flag_emoji_unicode": "U+1F1F8 U+1F1EC",
    "calling_code": "65",
    "is_eu": false
  }
}

限制:

  • 每月 10,000 个请求
  • 需要IP地址参数
  • 需要注册才能获取您的 API 密钥
  • 免费计划没有 SSL (https)

jsonip.com

 $.getJSON('https://jsonip.com/', function(data) {
  console.log(JSON.stringify(data, null, 2));
});
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

限制:

  • 如果有,则仅返回 IPv6 地址

JSON 测试

试试看:http: //ip.jsontest.com/

 $.getJSON('http://ip.jsontest.com/', function(data) {
  console.log(JSON.stringify(data, null, 2));
});

退货:

 {
  "ip": "116.12.250.1"
}

限制:

  • 无 SSL (https)
  • 如果有,则仅返回 IPv6 地址

史努比

 let apiKey = 'ed5ebbeba257b8f262a6a9bbc0ec678e';
$.getJSON('https://api.snoopi.io/116.12.250.1?apikey=' + apiKey, function(data) {
  console.log(JSON.stringify(data, null, 2));
});
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

限制:

  • 每月 10,000 个请求
  • 每 2 秒 1 个请求
  • 需要IP地址参数
  • 需要注册才能获取您的 API 密钥

香草JavaScript

使用现代浏览器,您可以使用本机 Fetch API 而不是依赖 jQuery 的 $.getJSON() 。这是一个例子:

 let apiKey = '1be9a6884abd4c3ea143b59ca317c6b2';
// Make the request
fetch('https://ipgeolocation.abstractapi.com/v1/?api_key=' + apiKey)
  // Extract JSON body content from HTTP response
  .then(response => response.json())
  // Do something with the JSON data
  .then(data => {
    console.log(JSON.stringify(data, null, 2))
  });

笔记

  • 由于这些都是免费服务,谁知道他们何时/是否会在未来离线(图表 A: Telize )。
  • 如果您需要更多功能和稳定性,大多数这些服务还提供付费套餐。
  • 正如@skobaljic 在下面的评论中指出的那样,请求配额主要是学术性的,因为调用发生在客户端,并且大多数最终用户永远不会超过他们的配额。
  • 某些服务没有可运行的代码段,因为它们在免费计划中不允许 SSL 连接或需要非空 Origin 请求标头(StackOverflow 代码段被迫使用 https 并且具有 Origin: null 在请求标头中)。

更新

  • 2/1/2016:移除 Telize (不再提供免费计划)
  • 2016 年 4 月 18 日:删除了 freegeoip.net (停止服务)
  • 2016 年 4 月 26 日:添加 了 DB-IP
  • 4/26/2016:添加 黑客目标
  • 7/6/2016:恢复 freegeoip.net
  • 2016 年 7 月 6 日:删除 了 ip-json.rhcloud.com (无效链接)
  • 12/21/2016:删除 黑客目标(停止服务)
  • 2/10/2017:添加 了 Nekudo
  • 4/20/2017:添加 了 ipapi (感谢 Ahmad Awais)
  • 4/24/2017:恢复 黑客目标
  • 2017 年 4 月 24 日:删除 Snoopi.io (停止服务)
  • 2017 年 7 月 16 日:添加限制“免费计划无 SSL (https)”
  • 7/16/2017:添加 IP 查找(感谢 JordanC)
  • 2017 年 9 月 25 日:添加 了 Stupid Web Tools (感谢 Cœur)
  • 3/16/2018:添加 了 ipdata (感谢 Jonathan)
  • 4/14/2018:将 freegeoip.net 重命名为 ipstack (感谢 MA-Maddin)
  • 2018 年 4 月 16 日:添加 了 GeoIPLookup.io (感谢 Rob Waa)
  • 6/11/2018:添加 了 ipgeolocation (感谢 Ejaz Ahmed)
  • 7/31/2019:添加 了 ipregistry (感谢 Laurent)
  • 2019 年 8 月 16 日:添加 了 SmartIP.io (感谢 kevinj)
  • 8/22/2019:删除了 Stupid Web Tools (停止服务)
  • 12/10/2019:添加 了 Cloudflare
  • 2020 年 1 月 9 日:删除 SmartIP.io (停止服务)
  • 11/6/2020:添加 摘要
  • 11/13/2020:添加 了 AstroIP.co
  • 2021 年 4 月 13 日:用片段替换代码示例(接近 30k 字符限制)
  • 2021 年 4 月 13 日:添加了将键值对转换为 JSON 以进行纯文本响应的代码
  • 2021 年 4 月 13 日:添加限制“需要非空 Origin 请求标头”
  • 2021 年 4 月 13 日:添加 了 BigDataCloud
  • 2021 年 4 月 13 日:恢复 Snoopi.io
  • 2021 年 4 月 13 日:删除了 AstroIP.co (停止服务)
  • 4/13/2021:删除 了 Nekudo (现在是 ipapi 的一部分)

原文由 thdoan 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题