主要记录一些日常开发中可以使用到的一些免费api接口,目前包括 ip地址查询、天气查询
通过 IP 查询地址
ip-api (不支持 https)
💡 api接口文档
🕹 调用接口
$ curl http://ip-api.com/json
📝 返回信息(位置信息,服务器的经纬度、时区)
{"query": "24.48.0.1","status": "success","country": "Canada","countryCode": "CA","region": "QC","regionName": "Quebec","city": "Montreal","zip": "H3V","lat": 45.4998,"lon": -73.6087,"timezone": "America/Toronto","isp": "Le Groupe Videotron Ltee","org": "Videotron Ltee","as": "AS5769 Videotron Telecom Ltee"
}
补充:
http://ip-api.com/json/ # 国际化英文显示
http://ip-api.com/json/?lang=zh-CN # 中文显示
http://ip-api.com/json/115.191.200.34?lang=zh-CN # 查询某个ip的信息
ipapi (支持 https)
💡 api接口文档
🕹 调用接口
$ curl https://ipapi.co/json/
📝 返回信息(位置信息,服务器的经纬度、时区)
{"ip": "8.8.8.8","network": "8.8.8.0/24","version": "IPv4","city": "Mountain View","region": "California","region_code": "CA","country": "US","country_name": "United States","country_code": "US","country_code_iso3": "USA","country_capital": "Washington","country_tld": ".us","continent_code": "NA","in_eu": false,"postal": "94043","latitude": 37.42301,"longitude": -122.083352,"timezone": "America/Los_Angeles","utc_offset": "-0800","country_calling_code": "+1","currency": "USD","currency_name": "Dollar","languages": "en-US,es-US,haw,fr","country_area": 9629091.0,"country_population": 327167434,"asn": "AS15169","org": "GOOGLE"
}
百度地图api (支持HTTP/HTTPS)
相对于上面提到的两个api,百度地图api适合在国内使用,不支持海外场景,返回的经纬度坐标是当前城市中心点,使用时需要先申请AK,获取到国内的详细地址信息,在有些场景下还是很有用的。如果使用场景需要精确的经纬度,可以再调用一个 百度api 的 地理编码,使用详细地址转换到对应经纬度。
💡 开发文档的地址
🕹 调用接口
// 其中需要注意的是: ak 是必填的,其他几个参数可选。
//HTTP协议
http://api.map.baidu.com/location/ip?ak=您的AK&ip=您的IP&coor=bd09ll //HTTPS协议
https://api.map.baidu.com/location/ip?ak=您的AK&ip=您的IP&coor=bd09ll
📝 返回信息(位置信息,服务器的经纬度、时区)
{"address": "CN|北京市|北京市|None|None|95|95","content": {"address": "北京市","address_detail": {"adcode": "110000","city": "北京市","city_code": 131,"district": "","province": "北京市","street": "","street_number": ""},"point": {"x": "116.41338370","y": "39.91092455"}},"status": 0
}
🗝 申请AK
创建应用
在创建应用时候可以勾选全部的基础服务
需要注意的是:ip白名单不建议使用 0.0.0.0/0 可以使用具体的ip名称。
天气查询 API
使用百度开放API,必须搭配AK使用,AK的获取方法上面已经提到了。
💡 开发文档的地址
🕹 调用接口
https://api.map.baidu.com/weather/v1/?district_id=222405&data_type=all&ak=你的ak
//GET请求
{"status": 0,"result": {"location": {"country": "中国","province": "吉林省","city": "延边朝鲜族自治州","name": "龙井","id": "222405"},"now": {"text": "多云","temp": -9,"feels_like": -13,"rh": 49,"wind_class": "3级","wind_dir": "西北风","prec_1h": 0,"clouds": 70,"vis": 8300,"aqi": 21,"pm25": 5,"pm10": 10,"no2": 4,"so2": 5,"o3": 67,"co": 0.5,"uptime": "20231124131000"},"indexes": [{"name": "晨练指数","brief": "较不宜","detail": "室外锻炼注意保暖避风防滑。"},{"name": "洗车指数","brief": "适宜","detail": "天气较好,适合擦洗汽车。"},{"name": "感冒指数","brief": "易发","detail": "天凉,易感冒"},{"name": "紫外线指数","brief": "强","detail": "涂擦 SPF20左右,PA++,避免强光。"},{"name": "穿衣指数","brief": "冷","detail": "建议着棉衣加羊毛衫等冬季服装。"},{"name": "运动指数","brief": "较不宜","detail": "推荐您在室内进行低强度运动。"}],"alerts": [],"forecasts": [{"text_day": "晴","text_night": "晴","high": -5,"low": -12,"wc_day": "3~4级","wd_day": "西风","wc_night": "3~4级","wd_night": "西风","date": "2023-11-24","week": "星期五"},....]}
}