
天气接口API文档
概述
本API提供实时及未来多日的天气预报服务,包括温度、湿度、风速、气压等详细气象信息。用户可以通过HTTP请求获取指定地理位置的天气数据。
基础URL
https://api.weatherprovider.com/v1/请求方式
- GET:用于获取天气数据。
身份验证
部分高级功能可能需要API密钥进行身份验证。请在请求头中包含以下字段:
Authorization: Bearer YOUR_API_KEY参数说明
公共参数
- lat(必需):目标位置的纬度,浮点数格式,例如39.9042。
- lon(必需):目标位置的经度,浮点数格式,例如116.4074。
- appid(某些情况下必需):API密钥,字符串格式,用于身份验证和配额管理。
- units(可选):单位系统,默认值为"metric"(公制),可设置为"imperial"(英制)或"standard"(标准)。
- lang(可选):返回数据的语言,默认为"en"(英语),支持多种国际语言代码,如"zh_cn"(简体中文)。
时间相关参数(适用于预报数据)
- days(可选):查询未来多少天的预报,整数格式,范围为1-15天,默认为1天。
接口列表
当前天气
- URL: /current
- 示例请求:GET https://api.weatherprovider.com/v1/current?lat=39.9042&lon=116.4074&units=metric&lang=zh_cn
- 响应示例:{ "coord": {"lon": 116.4074, "lat": 39.9042}, "weather": [{"description": "晴", "icon": "01d"}], "base": "stations", "main": { "temp": 285.15, // Kelvin温度,需转换为摄氏度或其他单位 "pressure": 1013, "humidity": 44, "temp_min": 283.15, "temp_max": 287.15 }, "visibility": 10000, "wind": {"speed": 2.1, "deg": 150}, "clouds": {"all": 1}, "dt": 1625097600, // 数据时间戳(UTC秒级) "sys": { "type": 1, "id": 5091, "message": 0.0037, "country": "CN", "sunrise": 1625052422, "sunset": 1625104379 }, "timezone": 28800, "id": 1816279, "name": "北京", "cod": 200 }
未来天气预报
- URL: /forecast
- 示例请求:GET https://api.weatherprovider.com/v1/forecast?lat=39.9042&lon=116.4074&units=metric&lang=zh_cn&days=7
- 响应示例:[ { "dt": 1625097600, // 数据时间戳(UTC秒级) "main": { "temp": 285.15, "temp_min": 283.15, "temp_max": 287.15, "pressure": 1013, "sea_level": 1028, "grnd_level": 1013, "humidity": 44, "temp_kf": 4 }, "weather": [{"description": "晴", "icon": "01d"}], "clouds": {"all": 1}, "wind": {"speed": 2.1, "deg": 150}, "pop": 0 }, // 更多天数的数据... ]
错误处理
- 400 Bad Request:请求参数错误或缺失。
- 401 Unauthorized:未提供有效的API密钥或API密钥无效。
- 404 Not Found:请求的资源不存在。
- 500 Internal Server Error:服务器内部错误。
联系我们
如有任何问题或需要更多帮助,请访问我们的官方网站或通过电子邮件联系技术支持团队:support@weatherprovider.com。
请根据实际需求调整API URL、参数及示例内容。
