今天项目中遇到个问题,要获取当天的天气信息。一开始打算调用google天气的接口,考虑到google随时可能被墙。所以改用了中国天气网(http://www.weather.com.cn/)的天气接口,据说这个比较准确。
中国天气网提供了几种可选的天气插件,可以参考:http://service.weather.com.cn/plugin/index.shtml
这些插件基本上能满足一般的需要,如果有特殊需要还可以自己定制插件。
这些插件使用也很简单,只要将你需要的插件的iframe代码嵌到你想要展示的网页位置即可。
下面介绍一下两个有用的天气数据接口:
第一个接口:http://m.weather.com.cn/data/101010100.html
其中101010100为城市id,附件中提供了所有的城市id。
这个接口返回一个城市未来七天(包括今天)的天气数据(json格式)。
可以使用PHP的json_decode将这个json串转换为数组,只需将json_decode的第二个参数设置为true
下面是我调用这个接口的代码:
$city_code = '101010100';
header("Content-Type: text/html; charset=UTF-8");
$weathe_url = 'http://m.weather.com.cn/data/' .$city_code. '.html';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $weathe_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
$wather_array = json_decode($data, true);
print_r($wather_array);
输出:
Array
(
[weatherinfo] => Array
(
[city] => 北京
[city_en] => beijing
[date_y] => 2012年3月11日
[date] =>
[week] => 星期日
[fchh] => 18
[cityid] => 101010100
[temp1] => -6℃~9℃
[temp2] => -3℃~13℃
[temp3] => 0℃~12℃
[temp4] => -2℃~9℃
[temp5] => 2℃~11℃
[temp6] => 2℃~9℃
[tempF1] => 21.2℉~48.2℉
[tempF2] => 26.6℉~55.4℉
[tempF3] => 32℉~53.6℉
[tempF4] => 28.4℉~48.2℉
[tempF5] => 35.6℉~51.8℉
[tempF6] => 35.6℉~48.2℉
[weather1] => 晴
[weather2] => 晴转多云
[weather3] => 晴
[weather4] => 多云
[weather5] => 阴
[weather6] => 多云
[img1] => 0
[img2] => 99
[img3] => 0
[img4] => 1
[img5] => 0
[img6] => 99
[img7] => 1
[img8] => 99
[img9] => 2
[img10] => 99
[img11] => 1
[img12] => 99
[img_single] => 0
[img_title1] => 晴
[img_title2] => 晴
[img_title3] => 晴
[img_title4] => 多云
[img_title5] => 晴
[img_title6] => 晴
[img_title7] => 多云
[img_title8] => 多云
[img_title9] => 阴
[img_title10] => 阴
[img_title11] => 多云
[img_title12] => 多云
[img_title_single] => 晴
[wind1] => 微风
[wind2] => 微风
[wind3] => 微风
[wind4] => 微风
[wind5] => 微风
[wind6] => 微风
[fx1] => 微风
[fx2] => 微风
[fl1] => 小于3级
[fl2] => 小于3级
[fl3] => 小于3级
[fl4] => 小于3级
[fl5] => 小于3级
[fl6] => 小于3级
[index] => 凉
[index_d] => 天气凉,建议着厚外套加毛衣等春秋服装。体弱者宜着大衣、呢外套。因昼夜温差较大,注意增减衣服。
[index48] => 凉
[index48_d] => 天气凉,建议着厚外套加毛衣等春秋服装。体弱者宜着大衣、呢外套。因昼夜温差较大,注意增减衣服。
[index_uv] => 中等
[index48_uv] => 弱
[index_xc] => 适宜
[index_tr] => 适宜
[index_co] => 较舒适
[st1] => 8
[st2] => -4
[st3] => 13
[st4] => -1
[st5] => 12
[st6] => 1
[index_cl] => 不宜
[index_ls] => 基本适宜
[index_ag] => 较易发
)
)
下面是应用这些数据的代码:
$result = array( 'city' =>$wather_array['weatherinfo']['city'], 'date' => $wather_array['weatherinfo']['date_y'], 'temp' =>$wather_array['weatherinfo']['temp1'], 'weather' => $wather_array['weatherinfo']['weather1'], 'img'=>'http://m.weather.com.cn/img/c' . $wather_array['weatherinfo']['img1']. '.gif', 'wind' => $wather_array['weatherinfo']['wind1'] );
这段代码返回了今天的天气信息的数据。需要注意的是:图片的地址这时应该为绝对地址。
第二个有用的接口是:http://www.weather.com.cn/data/sk/101010100.html
该接口返回了实时的天气信息的json数据。
用json_decode转换为数组后的数据为:
Array
(
[weatherinfo] => Array
(
[city] => 北京
[cityid] => 101010100
[temp] => 8
[WD] => 西南风
[WS] => 3级
[SD] => 11%
[WSE] => 3
[time] => 14:30
[isRadar] => 1
[Radar] => JC_RADAR_AZ9010_JB
)
)
附件:中国天气网城市代码