获取IP地址信息的免费API——ipstack
ipstack为地理位置API提供强大的实时IP,能够查找准确的位置数据并评估源自高风险IP地址的安全威胁。结果以毫秒为单位以JSON或XML格式提供。使用ipstack API,您将能够第一眼就找到网站访问者,并相应地调整您的用户体验和应用程序。
1、 注册获取免费的API访问密钥
♠ 点我注册 ♠
2、 API访问密钥
- API地址: http://api.ipstack.com/
- 请求格式: http://api.ipstack.com/**134.201.250.155**?access_key=**YOUR_ACCESS_KEY**
- 请求响应:
{
"ip": "134.201.250.155",
"type": "ipv4",
"continent_code": "NA",
"continent_name": "North America",
"country_code": "US",
"country_name": "United States",
"region_code": "CA",
"region_name": "California",
"city": "Huntington Beach",
"zip": "92648",
"latitude": 33.6746,
"longitude": -118.0086,
"location": {
"geoname_id": 5358705,
"capital": "Washington D.C.",
"languages": [
{
"code": "en",
"name": "English",
"native": "English"
}
],
"country_flag": "http://assets.ipstack.com/flags/us.svg",
...
"calling_code": "1",
"is_eu": false
3、 JSONP Callbacks
- 请求格式: http://api.ipstack.com/**134.201.250.155**?access_key=**YOUR_ACCESS_KEY**&callback=**Your_Function**
- 请求响应:
myfunction(
{
"ip": "134.201.250.155",
"type": "ipv4",
"continent_code": "NA",
"continent_name": "North America",
"country_code": "US",
"country_name": "United States",
"region_code": "CA",
"region_name": "California",
"city": "Huntington Beach",
"zip": "92648",
"latitude": 33.6746,
"longitude": -118.0086,
"location": {
"geoname_id": 5358705,
"capital": "Washington D.C.",
"languages": [
{
"code": "en",
"name": "English",
"native": "English"
}
],
"country_flag": "http://assets.ipstack.com/flags/us.svg",
...
"calling_code": "1",
"is_eu": false
}
}
)
4、错误代码
5、 示例代码
- PHP (cURL)
// set IP address and API access key
$ip = '134.201.250.155';
$access_key = 'YOUR_ACCESS_KEY';
// Initialize CURL:
$ch = curl_init('http://api.ipstack.com/'.$ip.'?access_key='.$access_key.'');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Store the data:
$json = curl_exec($ch);
curl_close($ch);
// Decode JSON response:
$api_result = json_decode($json, true);
// Output the "capital" object inside "location"
echo $api_result['location']['capital'];
- JavaScript (jQuery.ajax)
// set endpoint and your access key
var ip = '134.201.250.155'
var access_key = 'YOUR_ACCESS_KEY';
// get the API result via jQuery.ajax
$.ajax({
url: 'http://api.ipstack.com/' + ip + '?access_key=' + access_key,
dataType: 'jsonp',
success: function(json) {
// output the "capital" object inside "location"
alert(json.location.capital);
}
});