获取IP地址信息的免费API——ipstack

ipstack为地理位置API提供强大的实时IP,能够查找准确的位置数据并评估源自高风险IP地址的安全威胁。结果以毫秒为单位以JSON或XML格式提供。使用ipstack API,您将能够第一眼就找到网站访问者,并相应地调整您的用户体验和应用程序。

1、 注册获取免费的API访问密钥

点我注册

Tony's blog image

2、 API访问密钥

  1. {
  2. "ip": "134.201.250.155",
  3. "type": "ipv4",
  4. "continent_code": "NA",
  5. "continent_name": "North America",
  6. "country_code": "US",
  7. "country_name": "United States",
  8. "region_code": "CA",
  9. "region_name": "California",
  10. "city": "Huntington Beach",
  11. "zip": "92648",
  12. "latitude": 33.6746,
  13. "longitude": -118.0086,
  14. "location": {
  15. "geoname_id": 5358705,
  16. "capital": "Washington D.C.",
  17. "languages": [
  18. {
  19. "code": "en",
  20. "name": "English",
  21. "native": "English"
  22. }
  23. ],
  24. "country_flag": "http://assets.ipstack.com/flags/us.svg",
  25. ...
  26. "calling_code": "1",
  27. "is_eu": false

3、 JSONP Callbacks

  1. myfunction(
  2. {
  3. "ip": "134.201.250.155",
  4. "type": "ipv4",
  5. "continent_code": "NA",
  6. "continent_name": "North America",
  7. "country_code": "US",
  8. "country_name": "United States",
  9. "region_code": "CA",
  10. "region_name": "California",
  11. "city": "Huntington Beach",
  12. "zip": "92648",
  13. "latitude": 33.6746,
  14. "longitude": -118.0086,
  15. "location": {
  16. "geoname_id": 5358705,
  17. "capital": "Washington D.C.",
  18. "languages": [
  19. {
  20. "code": "en",
  21. "name": "English",
  22. "native": "English"
  23. }
  24. ],
  25. "country_flag": "http://assets.ipstack.com/flags/us.svg",
  26. ...
  27. "calling_code": "1",
  28. "is_eu": false
  29. }
  30. }
  31. )

4、错误代码

错误代码

5、 示例代码

  • PHP (cURL)
  1. // set IP address and API access key
  2. $ip = '134.201.250.155';
  3. $access_key = 'YOUR_ACCESS_KEY';
  4. // Initialize CURL:
  5. $ch = curl_init('http://api.ipstack.com/'.$ip.'?access_key='.$access_key.'');
  6. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  7. // Store the data:
  8. $json = curl_exec($ch);
  9. curl_close($ch);
  10. // Decode JSON response:
  11. $api_result = json_decode($json, true);
  12. // Output the "capital" object inside "location"
  13. echo $api_result['location']['capital'];
  • JavaScript (jQuery.ajax)
  1. // set endpoint and your access key
  2. var ip = '134.201.250.155'
  3. var access_key = 'YOUR_ACCESS_KEY';
  4. // get the API result via jQuery.ajax
  5. $.ajax({
  6. url: 'http://api.ipstack.com/' + ip + '?access_key=' + access_key,
  7. dataType: 'jsonp',
  8. success: function(json) {
  9. // output the "capital" object inside "location"
  10. alert(json.location.capital);
  11. }
  12. });

更多详细内容,请查看官方文档:
官方文档