开发者对接教程 + 在线接口调试工具 | 当前本机IP:216.73.216.138
本站接口路径:https://ip258.cn/api/ip_api.php
功能:传入IP地址,返回该IP归属地、运营商等完整信息
请求方式:GET
请求参数:ip 字符串,必填
# curl调用示例
curl https://ip258.cn/api/ip_api.php?ip=1.1.1.1
功能:无需传IP,自动获取当前访客出口公网IP
请求方式:GET
请求参数:myip=1
# curl调用示例
curl https://ip258.cn/api/ip_api.php?myip=1
<?php
// PHP请求接口示例
$ip = "1.1.1.1";
$apiUrl = "https://ip258.cn/api/ip_api.php?ip=".urlencode($ip);
$json = file_get_contents($apiUrl);
$data = json_decode($json,true);
print_r($data);
?>
// JS fetch请求示例
fetch('https://ip258.cn/api/ip_api.php?ip=1.1.1.1')
.then(res=>res.json())
.then(ret=>{
console.log(ret);
})
| 返回code | HTTP状态 | 含义 |
|---|---|---|
| 200 | 200 OK | 查询成功,返回IP归属地数据 |
| 400 | 200 OK | 参数错误,传入的IP不是合法IPv4/IPv6 |
| 403 | 403 Forbidden | 禁止访问:爬虫UA拦截 / IP位于黑名单 / 鉴权失败 |
| 429 | 200 OK | 请求限流,单IP60秒最多100次请求,请降速重试 |
| 500 | 200 OK | 上游查询接口异常,稍后重试 |
//接口统一错误处理示例
fetch('https://ip258.cn/api/ip_api.php?ip=1.1.1.1')
.then(res=>{
//处理HTTP层面403等错误状态,读取响应体中的JSON
return res.json().then(data=>{
if(!res.ok){
throw data; //把业务json抛给catch
}
return data;
});
})
.then(ret=>{
if(ret.code === 200){
console.log('成功',ret.data);
}else{
console.error('接口业务错误',ret.code,ret.msg);
}
})
.catch(err=>{
//捕获:网络异常、HTTP403黑名单/爬虫拦截返回的json错误
if(err && err.code){
console.error('接口异常',err.code,err.msg);
}else{
console.error('网络请求失败',err);
}
});