首页 >后端开发 >php教程 >php根据接口获取天气

php根据接口获取天气

WBOY
WBOY原创
2016-06-23 13:37:121059浏览

从网上找到一个大众的常用的天气接口:
上海:“ http://www.weather.com.cn/adat/cityinfo/101020100.html”
返回json子串:

{"weatherinfo":{"city":"上海","city_en":"shanghai","date_y":"2014年3月4日","date":"","week":"星期二","fchh":"11","cityid":"101020100","temp1":"11C~6C","temp2":"10C~5C","temp3":"8C~4C","temp4":"8C~6C","temp5":"8C~6C","temp6":"10C~5C","tempF1":"51.8F~42.8F","tempF2":"50F~41F","tempF3":"46.4F~39.2F","tempF4":"46.4F~42.8F","tempF5":"46.4F~42.8F","tempF6":"50F~41F","weather1":"小雨转多云","weather2":"多云转阴","weather3":"小雨","weather4":"小雨","weather5":"小雨转阴","weather6":"多云","img1":"7","img2":"1","img3":"1","img4":"2","img5":"7","img6":"99","img7":"7","img8":"99","img9":"7","img10":"2","img11":"1","img12":"99","img_single":"7","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":"东北风3-4级转北风4-5级","wind2":"北风4-5级转东北风3-4级","wind3":"东风4-5级","wind4":"东风转东北风4-5级","wind5":"东北风3-4级","wind6":"东北风转东风3-4级","fx1":"东北风","fx2":"北风","fl1":"3-4级转4-5级","fl2":"4-5级转3-4级","fl3":"4-5级","fl4":"4-5级","fl5":"3-4级","fl6":"3-4级","index":"较冷","index_d":"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。","index48":"较冷","index48_d":"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。","index_uv":"最弱","index48_uv":"最弱","index_xc":"不宜","index_tr":"适宜","index_co":"较舒适","st1":"10","st2":"3","st3":"9","st4":"2","st5":"7","st6":"2","index_cl":"不宜","index_ls":"不宜","index_ag":"易发"}}

内容格式倒是正确,信息也比较全, 可是没有指定日期的方式获取。
问题:
① 如何获取上海 当天和7天内的天气信息?
或者
② 能用正则将中国天气网的信息匹配出来?
url: http://www.weather.com.cn/weather/101020100.shtml
<div class="m m3" id="7d"><ul class="t clearfix"><li class='dn on' data-dn='7d1'><h1>今天</h1><h2>3日</h2> <!-- 正则获取标签内容 --><big class="jpg50 d07"></big> <!-- 正则获取class内容 --><big class="jpg50 n07"></big> <!-- 正则获取class内容 --><p class="wea">小雨</p><!-- 正则获取标签内容 --><p class="tem tem1"><span>16</span><i>C</i><!-- 正则获取标签内容 --></p><p class="tem tem2"><span>13</span><i>C</i><!-- 正则获取标签内容 --></p><p class="win"><em><span title="东北风" class="NE"></span><span title="东风" class="E"></span></em><i>微风</i><!-- 正则获取标签内容 --></p><div class="slid"></div></li><!-- 以下正则匹配规则同上 --><li class='dn' data-dn='7d2'><h1>明天</h1><h2>4日</h2><big class="jpg50 d07"></big><big class="jpg50 n07"></big><p class="wea">小雨</p><p class="tem tem1"><span>23</span><i>C</i></p><p class="tem tem2"><span>15</span><i>C</i></p><p class="win"><em><span title="东南风" class="SE"></span><span title="北风" class="N"></span></em><i>微风</i></p><div class="slid"></div></li><li class='dn' data-dn='7d3'><h1>后天</h1><h2>5日</h2><big class="jpg50 d04"></big><big class="jpg50 n04"></big><p class="wea">雷阵雨</p><p class="tem tem1"><span>21</span><i>C</i></p><p class="tem tem2"><span>13</span><i>C</i></p><p class="win"><em><span title="东北风" class="NE"></span><span title="东北风" class="NE"></span></em><i>微风</i></p><div class="slid"></div></li><li class='dn' data-dn='7d4'><h1>周一</h1><h2>6日</h2><big class="jpg50 d08"></big><big class="jpg50 n07"></big><p class="wea">中雨转小雨</p><p class="tem tem1"><span>15</span><i>C</i></p><p class="tem tem2"><span>10</span><i>C</i></p><p class="win"><em><span title="北风" class="N"></span><span title="北风" class="N"></span></em><i>3-4级</i></p><div class="slid"></div></li><li class='dn' data-dn='7d5'><h1>周二</h1><h2>7日</h2><big class="jpg50 d08"></big><big class="jpg50 n08"></big><p class="wea">中雨</p><p class="tem tem1"><span>13</span><i>C</i></p><p class="tem tem2"><span>8</span><i>C</i></p><p class="win"><em><span title="北风" class="N"></span><span title="北风" class="N"></span></em><i>3-4级</i></p><div class="slid"></div></li><li class='dn' data-dn='7d6'><h1>周三</h1><h2>8日</h2><big class="jpg50 d08"></big><big class="jpg50 n07"></big><p class="wea">中雨转小雨</p><p class="tem tem1"><span>13</span><i>C</i></p><p class="tem tem2"><span>9</span><i>C</i></p><p class="win"><em><span title="东北风" class="NE"></span><span title="东北风" class="NE"></span></em><i>微风</i></p><div class="slid"></div></li><li class='dn' data-dn='7d7'><h1>周四</h1><h2>9日</h2><big class="jpg50 d02"></big><big class="jpg50 n02"></big><p class="wea">阴</p><p class="tem tem1"><span>14</span><i>C</i></p><p class="tem tem2"><span>9</span><i>C</i></p><p class="win"><em><span title="东北风" class="NE"></span><span title="东北风" class="NE"></span></em><i>微风</i></p><div class="slid"></div></li></ul><i class="line1"></i></div>				<!-- 嵌套7天逐12小时精细化预报模块 开始-->			</div>

将我注释的的内容匹配出来就行,不过这个方法麻烦了点,效率也不高。
求助。。。


回复讨论(解决方案)

找到了一个实时的接口:
http://weatherapi.market.xiaomi.com/wtr-v2/weather?cityId=101020100

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn