PHP根据中国天气获取本地程式天气

来源:互联网 发布:淘宝复制宝贝有影响吗 编辑:程序博客网 时间:2024/06/05 08:58

中央气象台提供以下三种方式获取天气接口(101190801是城市代码):

http://www.weather.com.cn/data/sk/101190801.html
http://www.weather.com.cn/data/cityinfo/101190801.html
http://m.weather.com.cn/data/101190801.html

第三个接口获取最详细,我们解析第三个城市

$weather = file_get_contents( 'http://m.weather.com.cn/data/101190801.html' );$data = json_decode( $weather, true );echo '<pre>';print_r($data);echo '</pre>';

返回的数据是json格式的 反编译之后,我们可以很清晰的看到数据结构:

Array(    [weatherinfo] => Array        (            [city] => 徐州            [city_en] => xuzhou            [date_y] => 2013年12月30日            [date] =>             [week] => 星期一            [fchh] => 08            [cityid] => 101190801// 今天至后五天 温度            [temp1] => 9℃~-1℃ [temp2] => 10℃~-2℃[temp3] => 11℃~-2℃[temp4] => 11℃~-1℃[temp5] => 11℃~-2℃[temp6] => 9℃~-3℃            //华氏度[tempF1] => 48.2℉~30.2℉[tempF2] => 50℉~28.4℉[tempF3] => 51.8℉~28.4℉[tempF4] => 51.8℉~30.2℉[tempF5] => 51.8℉~28.4℉[tempF6] => 48.2℉~26.6℉            //天气情况[weather1] => 晴 [weather2] => 晴 [weather3] => 晴[weather4] => 晴 [weather5] => 晴 [weather6] => 晴            //天气图标 1-2当日的百天与晚上[img1] => 0 [img2] => 99 [img3] => 0 ...[img12] => 99//图标title            [img_title1] => 晴 [img_title2] => 晴[img_title3] => 晴 ...[img_title12] => 晴            //风速描述[wind1] => 西风3-4级 [wind2] => 西北风4-5级转西风3-4级 [wind3] => 西北风转西南风3-4级 [wind4] => 西南风3-4级 [wind5] => 西北风转北风3-4级 [wind6] => 西风转北风3-4级//风力            [fl1] => 3-4级 [fl2] => 4-5级转3-4级 [fl3] => 3-4级 [fl4] => 3-4级 [fl5] => 3-4级 [fl6] => 3-4级            //穿衣指数[index] => 冷            [index_d] => 天气冷,建议着棉服、羽绒服、皮夹克加羊毛衫等冬季服装。年老体弱者宜着厚棉衣、冬大衣或厚羽绒服。            //48小时穿衣指数[index48] => 冷            [index48_d] => 天气冷,建议着棉服、羽绒服、皮夹克加羊毛衫等冬季服装。年老体弱者宜着厚棉衣、冬大衣或厚羽绒服。            //紫外线指数 以及48小时紫外线指数[index_uv] => 中等            [index48_uv] => 中等//洗车指数            [index_xc] => 适宜//旅游指数            [index_tr] => 一般//舒适度指数            [index_co] => 较舒适//晨练指数            [index_cl] => 较不宜//晾晒指数            [index_ls] => 基本适宜//过敏指数            [index_ag] => 极不易发        ))

如何获取城市ID

1、直接在http://www.weather.com.cn/

2、通过接口获取(不能直接访问)

1. 通过 http://m.weather.com.cn/data5/city.xml 获取省份列表;
2. 通过 http://m.weather.com.cn/data5/city19.xml 获取该省份的城市列表,其中19为江苏省代号,1中所返回的;
3. 通过 http://m.weather.com.cn/data5/city2816.xml 获取该城市的区列表,其中2816为东莞市代号,2中所返回的;
4. 通过 http://m.weather.com.cn/data5/city281601.xml 获取最后的城市码,其中281601为3中返回;

$weather = file_get_contents('http://m.weather.com.cn/data5/city.xml');$w_arr = explode(',',$weather);echo '<pre>';print_r($w_arr);echo '</pre>';Array(    [0] => 01|北京    [1] => 02|上海    [2] => 03|天津    [3] => 04|重庆    [4] => 05|黑龙江    [5] => 06|吉林    [6] => 07|辽宁    [7] => 08|内蒙古    [8] => 09|河北...    [33] => 34|台湾)$weather = file_get_contents('http://m.weather.com.cn/data5/city19.xml');$w_arr = explode(',',$weather);echo '<pre>';print_r($w_arr);echo '</pre>';Array(    [0] => 1901|南京    [1] => 1902|无锡    [2] => 1903|镇江    [3] => 1904|苏州    [4] => 1905|南通...    [12] => 1913|宿迁)
有了这些数据 我们可以录入自己的数据库了 后面如何操作就因人而异了;



中国天气接口地址http://smart.weather.com.cn/wzfw/smart/weatherapi.shtml

0 0
原创粉丝点击