用户可通过行政区划代码查询实时天气信息及未来5天天气预报。
https://api.map.baidu.com/weather/v1/?district_id=222405&data_type=all&ak=你的ak
//get请求
参数名称 | 参数含义 | 默认值 | 字段类型 | 必选 |
---|---|---|---|---|
district_id | 区县的,和location二选一 | 无 | string | 否 |
location | 经纬度,经度在前纬度在后,逗号分隔。支持类型:bd09mc/bd09ll/wgs84/gcj02。开通后才能使用 | 无 | double | 否 |
ak | 开发者密钥,可在申请获得 | 无 | string | 是 |
data_type | 请求数据类型。数据类型有:now/fc/index/alert/fc_hour/all,控制返回内容 | 无 | string | 是 |
output | 返回格式,目前支持json/xml | json | string | 否 |
coordtype | 支持类型:wgs84/bd09ll/bd09mc/gcj02 | wgs84 | string | 否 |
注意:如果district_id和location同时传,默认以district_id为准;
参数名 | 参数类型 | 描述信息 | 返回条件 | 异常值 | 字段类型 |
---|---|---|---|---|---|
address | object | 地理位置信息 | - | - | 基础字段 |
country | string | 国家名称 | 始终返回 | - | 基础字段 |
province | string | 省份名称 | 始终返回 | - | 基础字段 |
city | string | 城市名称 | 始终返回 | - | 基础字段 |
name | string | 区县名称 | 始终返回 | - | 基础字段 |
id | string | 区县id | 始终返回 | - | 基础字段 |
now | object | 实况数据 | - | - | 基础字段 |
temp | int | 温度(℃) | 始终返回 | 999999 | 基础字段 |
feels_like | int | 体感温度(℃) | data_type=now/all | 999999 | 基础字段 |
rh | int | 相对湿度(%) | data_type=now/all | 999999 | 基础字段 |
wind_class | string | 风力等级 | data_type=now/all | 暂无 | 基础字段 |
wind_dir | string | 风向描述 | data_type=now/all | 暂无 | 基础字段 |
text | string | 天气现象 | data_type=now/all | 暂无 | 基础字段 |
prec_1h | double | 1小时累计降水量(mm) | data_type=now/all | 999999 | 高级字段 |
clouds | int | 云量(%) | data_type=now/all | 999999 | 高级字段 |
vis | int | 能见度(m) | data_type=now/all | 999999 | 高级字段 |
aqi | int | 空气质量指数数值 | data_type=now/all | 999999 | 高级字段 |
pm25 | int | pm2.5浓度(μg/m3) | data_type=now/all | 999999 | 高级字段 |
pm10 | int | pm10浓度(μg/m3) | data_type=now/all | 999999 | 高级字段 |
no2 | int | 二氧化氮浓度(μg/m3) | data_type=now/all | 999999 | 高级字段 |
so2 | int | 二氧化硫浓度(μg/m3) | data_type=now/all | 999999 | 高级字段 |
o3 | int | 臭氧浓度(μg/m3) | data_type=now/all | 999999 | 高级字段 |
co | double | 一氧化碳浓度(mg/m3) | data_type=now/all | 999999 | 高级字段 |
uptime | string | 数据更新时间,北京时间 | data_type=now/all | - | 基础字段 |
alert | objectarray | 气象预警数据 | - | - | 高级字段 |
type | string | 预警事件类型 | data_type=alert/all | 暂无 | 高级字段 |
level | string | 预警事件等级 | data_type=alert/all | 暂无 | 高级字段 |
title | string | 预警标题 | data_type=alert/all | - | 高级字段 |
desc | string | 预警详细提示信息 | data_type=alert/all | - | 高级字段 |
indexes | objectarray | 生活指数数据 | - | - | 高级字段 |
name | string | 生活指数中文名称 | data_type=index/all | 暂无 | 高级字段 |
brief | string | 生活指数概要说明 | data_type=index/all | 暂无 | 高级字段 |
detail | string | 生活指数详细说明 | data_type=index/all | 暂无 | 高级字段 |
forecasts | objectarray | 预报数据 | - | - | 基础字段 |
date | string | 日期,北京时区 | data_type=fc/all | - | 基础字段 |
week | string | 星期,北京时区 | data_type=fc/all | - | 基础字段 |
high | int | 最高温度(℃) | data_type=fc/all | 999999 | 基础字段 |
low | int | 最低温度(℃) | data_type=fc/all | 999999 | 基础字段 |
wc_day | string | 白天风力 | data_type=fc/all | 暂无 | 基础字段 |
wc_night | string | 晚上风力 | data_type=fc/all | 暂无 | 基础字段 |
wd_day | string | 白天风向 | data_type=fc/all | 暂无 | 基础字段 |
wd_night | string | 晚上风向 | data_type=fc/all | 暂无 | 基础字段 |
text_day | string | 白天天气现象 | data_type=fc/all | 暂无 | 基础字段 |
text_night | string | 晚上天气现象 | data_type=fc/all | 暂无 | 基础字段 |
未来24小时逐小时预报返回参数
参数名 | 参数类型 | 描述信息 | 返回条件 | 异常值 | 字段类型 |
---|---|---|---|---|---|
forecast_hours | object array | 预报数据 | - | - | 高级字段 |
text | string | 天气现象 | data_type=fc_hour/all | "暂无" | 高级字段 |
temp_fc | int | 温度(℃) | data_type=fc_hour/all | 999999 | 高级字段 |
wind_class | string | 风力等级 | data_type=fc_hour/all | "暂无" | 高级字段 |
wind_dir | string | 风向描述 | data_type=fc_hour/all | "暂无" | 高级字段 |
rh | int | 相对湿度 | data_type=fc_hour/all | 999999 | 高级字段 |
prec_1h | double | 1小时累计降水量(mm) | data_type=fc_hour/all | 999999 | 高级字段 |
clouds | int | 云量(%) | data_type=fc_hour/all | 999999 | 高级字段 |
data_time | string | 数据时间 | data_type=fc_hour/all | 999999 | 高级字段 |
输入ak可生成包含ip/sn校验方式的代码
<?php// 此处填写你在控制台-应用管理-创建应用后获取的ak$ak ='您的ak';// 发起一个http get请求,并返回请求的结果// $url字段为请求的地址// $param字段为请求的参数functionrequest_get($url ='', $param =array()){if(empty($url)||empty($param)){returnfalse;}$geturl = $url ."?".http_build_query($param);$curl =curl_init();// 初始化curlcurl_setopt($curl,curlopt_ssl_verifypeer,false);// 跳过证书检查curl_setopt($curl,curlopt_ssl_verifyhost,2);// 从证书中检查ssl加密算法是否存在curl_setopt($curl,curlopt_url, $geturl);// 抓取指定网页curl_setopt($curl,curlopt_timeout,1000);// 设置超时时间1秒curl_setopt($curl,curlopt_returntransfer,1);// curl不直接输出到屏幕curl_setopt($curl,curlopt_header,0);// 设置header$data =curl_exec($curl);// 运行curlif(!$data){print("an error occured in function request_get(): ".curl_error($curl)."\n");}curl_close($curl);return $data;}// 请求地址$url ='https://api.map.baidu.com/weather/v1/';// 构造请求参数$param['district_id']='222405';$param['data_type']='all';$param['ak']= $ak;$res =request_get($url, $param);// 将原始返回的结果打印出来print("请求的原始返回结果为:\n");print($res ."\n");?>
{"status":0,"result":{"location":{"country":"中国","province":"北京市","city":"北京市","name":"东城","id":"110101"},"now":{"temp":4,"feels_like":1,"rh":73,"wind_class":"2级","wind_dir":"东风","text":"多云","prec_1h":0,"clouds":999999,"vis":3471,"aqi":140,"pm25":107,"pm10":0,"no2":23,"so2":22,"o3":70,"co":1.7,"uptime":"20200220143500"},"indexes":[{"name":"晨练指数","brief":"较适宜","detail":"天气阴沉,请避免在林中晨练。"},{"name":"洗车指数","brief":"适宜","detail":"天气较好,适合擦洗汽车。"},{"name":"感冒指数","brief":"易发","detail":"天凉,昼夜温差大,易感冒"},{"name":"紫外线指数","brief":"最弱","detail":"辐射弱,涂擦spf8-12防晒护肤品。"},{"name":"穿衣指数","brief":"较冷","detail":"建议着厚外套加毛衣等服装。"},{"name":"运动指数","brief":"较适宜","detail":"气温较低,在户外运动请注意增减衣物。"}],"alerts":[{"type":"道路冰雪","level":"蓝色预警","title":"市气象局发布道路冰雪蓝色预警[iv级/一般]","desc": "市气象局发布道路冰雪蓝色预警信号:受降雪天气影响,预计未来 24 小时我市将出现对交通有影响的道路结冰或积雪,请有关部门及广大群众做好防范工作。"}],"forecasts":[{"date":"2020-02-20","week":"星期四","high":7,"low":-2,"wc_day":"<3级","wc_night":"<3级","wd_day":"东南风","wd_night":"北风","text_day":"多云","text_night":"阴","aqi":93},{"date":"2020-02-21","week":"星期五","high":11,"low":1,"wc_day":"4~5级","wc_night":"<3级","wd_day":"西北风","wd_night":"西北风","text_day":"多云","text_night":"晴","aqi":44},{"date":"2020-02-22","week":"星期六","high":10,"low":-2,"wc_day":"<3级","wc_night":"<3级","wd_day":"西风","wd_night":"南风","text_day":"晴","text_night":"晴","aqi":39},{"date":"2020-02-23","week":"星期日","high":11,"low":0,"wc_day":"<3级","wc_night":"<3级","wd_day":"北风","wd_night":"北风","text_day":"晴","text_night":"晴","aqi":65},{"date":"2020-02-24","week":"星期一","high":9,"low":-1,"wc_day":"<3级","wc_night":"<3级","wd_day":"东风","wd_night":"东北风","text_day":"多云","text_night":"多云","aqi":38},{"date":"2020-02-25","week":"星期二","high":9,"low":-3,"wc_day":"<3级","wc_night":"<3级","wd_day":"东南风","wd_night":"西南风","text_day":"晴","text_night":"晴","aqi":27},{"date":"2020-02-26","week":"星期三","high":9,"low":-3,"wc_day":"<3级","wc_night":"<3级","wd_day":"西南风","wd_night":"西南风","text_day":"晴","text_night":"晴","aqi":26}],"forecast_hours":[{"text":"晴","temp_fc":14,"wind_class":"3~4级","wind_dir":"西南风","rh":15,"prec_1h":0,"clouds":10,"data_time":"2020-04-01 16:00:00"},{"text":"晴","temp_fc":14,"wind_class":"3~4级","wind_dir":"西南风","rh":13,"prec_1h":0,"clouds":10,"data_time":"2020-04-01 17:00:00"},{"text":"晴","temp_fc":13,"wind_class":"<3级","wind_dir":"西南风","rh":14,"prec_1h":0,"clouds":10,"data_time":"2020-04-01 18:00:00"},{"text":"晴","temp_fc":11,"wind_class":"<3级","wind_dir":"西南风","rh":15,"prec_1h":0,"clouds":10,"data_time":"2020-04-01 19:00:00"},{"text":"晴","temp_fc":10,"wind_class":"<3级","wind_dir":"西南风","rh":16,"prec_1h":0,"clouds":10,"data_time":"2020-04-01 20:00:00"},{"text":"晴","temp_fc":9,"wind_class":"<3级","wind_dir":"西风","rh":18,"prec_1h":0,"clouds":6,"data_time":"2020-04-01 21:00:00"},{"text":"晴","temp_fc":9,"wind_class":"<3级","wind_dir":"西风","rh":20,"prec_1h":0,"clouds":3,"data_time":"2020-04-01 22:00:00"},{"text":"晴","temp_fc":8,"wind_class":"<3级","wind_dir":"西风","rh":21,"prec_1h":0,"clouds":0,"data_time":"2020-04-01 23:00:00"},{"text":"晴","temp_fc":7,"wind_class":"<3级","wind_dir":"西北风","rh":26,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 00:00:00"},{"text":"晴","temp_fc":6,"wind_class":"<3级","wind_dir":"西北风","rh":31,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 01:00:00"},{"text":"晴","temp_fc":6,"wind_class":"<3级","wind_dir":"西北风","rh":36,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 02:00:00"},{"text":"晴","temp_fc":5,"wind_class":"<3级","wind_dir":"西北风","rh":39,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 03:00:00"},{"text":"晴","temp_fc":4,"wind_class":"<3级","wind_dir":"西北风","rh":42,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 04:00:00"},{"text":"晴","temp_fc":4,"wind_class":"<3级","wind_dir":"西北风","rh":45,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 05:00:00"},{"text":"晴","temp_fc":5,"wind_class":"<3级","wind_dir":"西北风","rh":40,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 06:00:00"},{"text":"晴","temp_fc":7,"wind_class":"<3级","wind_dir":"西北风","rh":34,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 07:00:00"},{"text":"晴","temp_fc":8,"wind_class":"<3级","wind_dir":"西北风","rh":29,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 08:00:00"},{"text":"晴","temp_fc":11,"wind_class":"<3级","wind_dir":"西北风","rh":29,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 09:00:00"},{"text":"晴","temp_fc":13,"wind_class":"<3级","wind_dir":"西北风","rh":29,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 10:00:00"},{"text":"晴","temp_fc":16,"wind_class":"<3级","wind_dir":"西北风","rh":29,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 11:00:00"},{"text":"晴","temp_fc":17,"wind_class":"3~4级","wind_dir":"西北风","rh":24,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 12:00:00"},{"text":"晴","temp_fc":18,"wind_class":"3~4级","wind_dir":"西北风","rh":19,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 13:00:00"},{"text":"晴","temp_fc":19,"wind_class":"3~4级","wind_dir":"西北风","rh":14,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 14:00:00"},{"text":"晴","temp_fc":19,"wind_class":"<3级","wind_dir":"西风","rh":17,"prec_1h":0,"clouds":0,"data_time":"2020-04-02 15:00:00"}]},"message":"success"}
参数 | 值 | 备注 | 必选 |
---|---|---|---|
district_id | 区县的,和location二选一 | 否 | |
data_type | 请求数据类型。数据类型有:now/fc/index/alert/fc_hour/all,控制返回内容 | 是 |
1 | 服务器内部错误 | 具体失败消息会在message提示 |
2 | 高级权限才允许使用location字段 | 用户需开通高级权限功能 |
10 | 缺少必要的请求参数 | 缺少参数的时候会抛出该异常 |
11 | 请求参数格式错误 | 请求参数类型错误时会抛出该异常, 如字符长度、数据类型、数据格式不规范等 |
20 | 身份验证失败 | ak 无效时会抛出该异常 |
21 | timestamp超出有效时间范围 | timestamp 与服务端的当前时间相差超过1分钟 |
40 | 查询的区域编码无效 | 查询的区域编码与约定的编码不符 |
41 | 查询的经纬度值范围无效 | 查询的经纬度超出了正常的经纬度范围 |
42 | 不支持的数据类型 | 查询的数据类型不支持 |
43 | 不支持的语言类型 | 查询的语言类型不支持 |
44 | 经纬度所在地区无数据覆盖 | 输入的经纬度区域无数据覆盖 |
45 | 服务被禁用,请确认是否开通高级权限 | 查询的数据类型与用户类型不匹配,请确认是否开通高级权限 |
50 | 服务繁忙 | 其他类型错误 |
101 | ak参数不存在 | 请求消息没有携带ak参数 |
102 | mcode参数不存在,mobile类型mcode参数必需 | 对于mobile类型的应用请求需要携带mcode参数,该错误码代表服务器没有解析到mcode |
200 | app不存在,ak有误请检查再重试 | 根据请求的ak,找不到对应的app |
201 | app被用户自己禁用,请在控制台解禁 | |
202 | app被管理员删除 | 恶意app被管理员删除 |
203 | app类型错误 | 当前api控制台支持server(类型1), mobile(类型2, 新版控制台区分为mobile_android(类型21)及mobile_iphone(类型22))及browser(类型3),除此之外其他类型认为是app类型错误 |
210 | app ip校验失败 | 在申请server类型应用的时候选择ip校验,需要填写ip白名单,如果当前请求的ip地址不在ip白名单或者不是0.0.0.0/0就认为ip校验失败 |
211 | app sn校验失败 | server类型app有两种校验方式ip校验和sn校验,当用户请求的sn和服务端计算出来的sn不相等的时候提示sn校验失败 |
220 | app referer校验失败 | 浏览器类型的app会校验referer字段是否存且切在referer白名单里面,否则返回该错误码 |
230 | app mcode码校验失败 | 服务器能解析到mcode,但和数据库中不一致,请携带正确的mcode |
240 | app 服务被禁用 | 用户在api控制台中创建或设置某app的时候禁用了某项服务,若需开通权限,可进入api控制台为ak勾选对应服务 |
250 | 用户不存在 | 根据请求的user_id, 数据库中找不到该用户的信息,请携带正确的user_id |
251 | 用户被自己删除 | 该用户处于未激活状态 |
252 | 用户被管理员删除 | 恶意用户被加入黑名单 |
260 | 服务不存在 | 服务器解析不到用户请求的服务名称 |
261 | 服务被禁用 | 该服务已下线,请在控制台确认是否有该服务的权限 |
301 | 永久配额超限,限制访问 | 配额超限,可在控制台 |
302 | 天配额超限,限制访问 | 配额超限,可在控制台 |
401 | 当前并发量已经超过约定并发配额,限制访问 | 并发控制超限,可在控制台 |
402 | 当前并发量已经超过约定并发配额,并且服务总并发量也已经超过设定的总并发配额,限制访问 | 并发控制超限,可在控制台 |
上一篇
下一篇
本篇文章对您是否有帮助?