接口简介

通过 IP 地址查询地理位置信息,返回国家、省份、城市、ISP、行政区划代码、经纬度、时区及当地天气等完整信息。

接口说明

基础信息

项目说明
接口路径https://api.lwcat.cn/api/ip/
请求方式GET
认证方式无需认证
返回格式JSON
字符编码UTF-8
CORS支持(*

请求参数

参数名类型必填说明
ipstring待查询的 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"

返回结构

顶层结构

字段类型说明
codeint状态码,0 为成功,1 为失败
dataobject业务数据,仅 code=0 时存在
messagestring错误信息,仅 code=1 时存在

data 字段说明

字段类型说明
ipstring查询的 IP 地址
countrystring国家,如 中国United States;特殊用途地址为 Reserved
provincestring省份,如 广东省;国外为州/省名;无数据时为空
citystring城市,如 中山市;无数据时为 0 或空
ispstring运营商,如 电信移动Google LLC;无数据时为 0 或空
adcodeint国标行政区划代码(6 位),如 442000;国外及特殊地址返回 0
latitudefloat|null纬度;私网/回环等无数据时为 null
longitudefloat|null经度;私网/回环等无数据时为 null
timezonestring时区,如 Asia/Shanghai;无数据时为空字符串
weatherobject|array天气信息;有经纬度时为对象,无经纬度或查询失败时为空数组 []
regionstringip2region 原始地区字符串(兼容保留字段)

weather 字段说明

字段类型说明
temperaturefloat|null当前温度(℃)
feels_likefloat|null体感温度(℃)
humidityint|null相对湿度(%)
precipitationfloat|null降水量(mm)
weather_codeintWMO 天气代码
weather_descstring天气中文描述,如 小阵雨雷暴
cloud_coverint|null云量(%)
pressurefloat|null气压(hPa)
wind_speedfloat|null风速(km/h)
wind_directionstring风向中文方位,如 东南西北
wind_degint|null风向角度(0-360)
wind_gustsfloat|null阵风风速(km/h)
is_daybool是否为白天

WMO 天气代码对照

代码描述代码描述
061小雨
1多云63中雨
2少云65大雨
366冻雨
4567强冻雨
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 请求、内部异常等)

注意事项

  1. 经纬度精度:IP 定位的经纬度为城市中心点级别,非精确定位
  2. 天气时效性:天气为查询时刻的实时数据,无缓存
  3. 客户端 IP 识别:接口依次从 X-Forwarded-ForX-Real-IPCLIENT_IPREMOTE_ADDR 获取客户端 IP
  4. CORS 支持:已配置跨域,前端可直接调用

添加新评论

文章目录