接入教程
1. 注册Abstract账户获取API密钥
2. 构造API请求URL:https://ipgeolocation.abstractapi.com/v1/?api_key=YOUR_KEY&ip_address=目标IP
3. 发送HTTP GET请求获取JSON格式的地理位置数据
4. 解析响应中的国家、城市、经纬度等信息
5. 可选:添加参数获取货币、时区等扩展数据
Python请求示例
import requests
api_key = 'YOUR_API_KEY'
base_url = 'https://ipgeolocation.abstractapi.com/v1/'
# 查询IP地址的地理位置
ip_address = '8.8.8.8'
params = {
'api_key': api_key,
'ip_address': ip_address
}
response = requests.get(base_url, params=params)
data = response.json()
if response.status_code == 200:
print(f"国家: {data.get('country')}")
print(f"城市: {data.get('city')}")
print(f"地区: {data.get('region')}")
print(f"货币: {data.get('currency', {}).get('currency_code')}")
else:
print(f"请求失败: {data.get('error', {}).get('message')}")
PHP请求示例
<?php
$api_key = 'YOUR_API_KEY';
$base_url = 'https://ipgeolocation.abstractapi.com/v1/';
// 查询IP地址的地理位置
$ip_address = '8.8.8.8';
$params = [
'api_key' => $api_key,
'ip_address' => $ip_address
];
$query_string = http_build_query($params);
$url = $base_url . '?' . $query_string;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
if ($data && isset($data['country'])) {
echo "国家: " . $data['country'] . "\n";
echo "城市: " . $data['city'] . "\n";
echo "地区: " . $data['region'] . "\n";
echo "货币: " . ($data['currency']['currency_code'] ?? '') . "\n";
} else {
echo "请求失败: " . ($data['error']['message'] ?? '未知错误') . "\n";
}
?>
JavaScript请求示例
const apiKey = 'YOUR_API_KEY';
const baseUrl = 'https://ipgeolocation.abstractapi.com/v1/';
// 查询IP地址的地理位置
const ipAddress = '8.8.8.8';
const params = new URLSearchParams({
api_key: apiKey,
ip_address: ipAddress
});
fetch(`${baseUrl}?${params}`)
.then(response => response.json())
.then(data => {
if (data.country) {
console.log(`国家: ${data.country}`);
console.log(`城市: ${data.city}`);
console.log(`地区: ${data.region}`);
console.log(`货币: ${data.currency?.currency_code || ''}`);
} else {
console.log(`请求失败: ${data.error?.message || '未知错误'}`);
}
})
.catch(error => {
console.error('网络请求错误:', error);
});
常见问题
这个API支持哪些IP地址格式?
Abstract IP地理位置API同时支持IPv4和IPv6地址格式。您可以使用标准的IPv4地址(如192.168.1.1)或IPv6地址(如2001:0db8:85a3:0000:0000:8a2e:0370:7334)进行查询。
API响应中包含哪些地理位置信息?
API响应包含IP地址对应的国家、城市、地区、经纬度坐标等信息。此外,还可以获取货币代码、国旗图标、语言等关联数据,具体取决于您的订阅计划。
如何处理API请求限制和错误?
建议检查响应状态码:200表示成功,400表示请求参数错误,401表示API密钥无效,429表示超过请求频率限制。请合理控制请求频率,并使用try-catch或错误处理机制捕获异常。
Aitishiku.com