接口简介
通过 IP 地址查询地理位置信息,返回国家、省份、城市、ISP、行政区划代码、经纬度、时区及当地天气等完整信息。
接口说明
基础信息
| 项目 | 说明 |
|---|
| 接口路径 | https://api.lwcat.cn/api/ip/ |
| 请求方式 | GET |
| 认证方式 | 无需认证 |
| 返回格式 | JSON |
| 字符编码 | UTF-8 |
| CORS | 支持(*) |
请求参数
| 参数名 | 类型 | 必填 | 说明 |
|---|
ip | string | 否 | 待查询的 IPv4/IPv6 地址。未传时自动获取客户端真实 IP(支持反向代理) |
请求示例
# 查询指定 IP
curl "https://api.lwcat.cn/api/ip/?ip=61.142.118.231"
# 自动获取客户端 IP
curl "https://api.lwcat.cn/api/ip/"
# 查询 IPv6 地址
curl "https://api.lwcat.cn/api/ip/?ip=2400:3200::1"
返回结构
顶层结构
| 字段 | 类型 | 说明 |
|---|
code | int | 状态码,0 为成功,1 为失败 |
data | object | 业务数据,仅 code=0 时存在 |
message | string | 错误信息,仅 code=1 时存在 |
data 字段说明
| 字段 | 类型 | 说明 |
|---|
ip | string | 查询的 IP 地址 |
country | string | 国家,如 中国、United States;特殊用途地址为 Reserved 等 |
province | string | 省份,如 广东省;国外为州/省名;无数据时为空 |
city | string | 城市,如 中山市;无数据时为 0 或空 |
isp | string | 运营商,如 电信、移动、Google LLC;无数据时为 0 或空 |
adcode | int | 国标行政区划代码(6 位),如 442000;国外及特殊地址返回 0 |
latitude | float|null | 纬度;私网/回环等无数据时为 null |
longitude | float|null | 经度;私网/回环等无数据时为 null |
timezone | string | 时区,如 Asia/Shanghai;无数据时为空字符串 |
weather | object|array | 天气信息;有经纬度时为对象,无经纬度或查询失败时为空数组 [] |
region | string | ip2region 原始地区字符串(兼容保留字段) |
weather 字段说明
| 字段 | 类型 | 说明 |
|---|
temperature | float|null | 当前温度(℃) |
feels_like | float|null | 体感温度(℃) |
humidity | int|null | 相对湿度(%) |
precipitation | float|null | 降水量(mm) |
weather_code | int | WMO 天气代码 |
weather_desc | string | 天气中文描述,如 晴、小阵雨、雷暴 |
cloud_cover | int|null | 云量(%) |
pressure | float|null | 气压(hPa) |
wind_speed | float|null | 风速(km/h) |
wind_direction | string | 风向中文方位,如 东南、西北 |
wind_deg | int|null | 风向角度(0-360) |
wind_gusts | float|null | 阵风风速(km/h) |
is_day | bool | 是否为白天 |
WMO 天气代码对照
| 代码 | 描述 | 代码 | 描述 |
|---|
| 0 | 晴 | 61 | 小雨 |
| 1 | 多云 | 63 | 中雨 |
| 2 | 少云 | 65 | 大雨 |
| 3 | 阴 | 66 | 冻雨 |
| 45 | 雾 | 67 | 强冻雨 |
| 48 | 雾凇 | 71 | 小雪 |
| 51 | 小毛毛雨 | 73 | 中雪 |
| 53 | 中毛毛雨 | 75 | 大雪 |
| 55 | 大毛毛雨 | 77 | 雪粒 |
| 56 | 冻毛毛雨 | 80 | 小阵雨 |
| 57 | 强冻毛毛雨 | 81 | 中阵雨 |
| - | - | 82 | 大阵雨 |
| - | - | 85 | 小阵雪 |
| - | - | 86 | 大阵雪 |
| - | - | 95 | 雷暴 |
| - | - | 96 | 雷暴伴小冰雹 |
| - | - | 99 | 雷暴伴大冰雹 |
响应示例
成功 - 国内 IPv4
{
"code": 0,
"data": {
"ip": "61.142.118.231",
"country": "中国",
"province": "广东省",
"city": "中山市",
"isp": "电信",
"adcode": 442000,
"latitude": 23.0242,
"longitude": 113.1334,
"timezone": "Asia/Shanghai",
"weather": {
"temperature": 29.6,
"feels_like": 34.7,
"humidity": 73,
"precipitation": 0.4,
"weather_code": 80,
"weather_desc": "小阵雨",
"cloud_cover": 54,
"pressure": 1008.3,
"wind_speed": 12.5,
"wind_direction": "东南",
"wind_deg": 143,
"wind_gusts": 41.8,
"is_day": true
},
"region": ""
}
}
成功 - 国外 IP
{
"code": 0,
"data": {
"ip": "8.8.8.8",
"country": "United States",
"province": "California",
"city": "0",
"isp": "Google LLC",
"adcode": 0,
"latitude": 37.751,
"longitude": -97.822,
"timezone": "America/Chicago",
"weather": {
"temperature": 28.5,
"feels_like": 28,
"humidity": 45,
"precipitation": 0,
"weather_code": 0,
"weather_desc": "晴",
"cloud_cover": 0,
"pressure": 1012.9,
"wind_speed": 16.4,
"wind_direction": "东北",
"wind_deg": 50,
"wind_gusts": 27.7,
"is_day": false
},
"region": ""
}
}
成功 - IPv6
{
"code": 0,
"data": {
"ip": "2400:3200::1",
"country": "中国",
"province": "浙江省",
"city": "杭州市",
"isp": "阿里",
"adcode": 330100,
"latitude": 30.2936,
"longitude": 120.1614,
"timezone": "Asia/Shanghai",
"weather": { },
"region": ""
}
}
成功 - 私网/保留地址
{
"code": 0,
"data": {
"ip": "192.168.1.1",
"country": "Reserved",
"province": "Reserved",
"city": "Reserved",
"isp": "0",
"adcode": 0,
"latitude": null,
"longitude": null,
"timezone": "",
"weather": [],
"region": ""
}
}
失败 - 无效 IP
{
"code": 1,
"message": "无效的 IP 地址: abc"
}
失败 - 查询异常
{
"code": 1,
"message": "查询失败: <异常信息>"
}
错误码
| code | 说明 |
|---|
0 | 查询成功 |
1 | 查询失败(无效 IP、非 GET 请求、内部异常等) |
注意事项
- 经纬度精度:IP 定位的经纬度为城市中心点级别,非精确定位
- 天气时效性:天气为查询时刻的实时数据,无缓存
- 客户端 IP 识别:接口依次从
X-Forwarded-For、X-Real-IP、CLIENT_IP、REMOTE_ADDR 获取客户端 IP - CORS 支持:已配置跨域,前端可直接调用