php获取各城市72小时天气预报[中国天气网]
时间:2021-07-01 10:21:17
帮助过:38人阅读
php获取各城市72小时天气预报内容
获取各城市天气预报内容data.txt下载:http://3.ibtf.sinaapp.com/data.txt[注意网页编码]
演示地址:
http://3.ibtf.sinaapp.com/?city=%E7%9F%B3%E5%AE%B6%E5%BA%84
- function getweather($city,$data='data.txt')//获取天气预报内容
- {
- $urlarr=unserialize(file_get_contents($data));
- if($urlarr[$city])
- {
- $url = $urlarr[$city];$text=$city;
- $lines_string = file_get_contents($url);
- $lines_string = explode("','',$lines_string[1]),str_replace('2-->','',$lines_string[2]),str_replace('3-->','',$lines_string_3[0]));
- for($i=0;$i< count($lines_array); $i ++)
- {
- $tiqian=array("℃","高温","低温");$tihou=array("度","","");$nowarray=str_replace($tiqian,$tihou,strip_tags($lines_array[$i]));
- $datearray=explode("日",$nowarray);$wtext[$i]=trim($datearray[0])."日";//获取日期
- $weather=explode("白天",$nowarray);$weather=explode("夜间",$weather[1]);
- $baiarr=wchangearray(explode("r",$weather[0]));//白天天气
- $yearr=wchangearray(explode("r",$weather[1]));//夜间天气
- if($baiarr[0]==$yearr[0]){$wtext[$i].=$baiarr[0];}else{$wtext[$i].=$baiarr[0]."转".$yearr[0];}//将天气添加到返回值里
- $wtext[$i].=$baiarr[1]."到".$yearr[1];//将气温添加到返回值里
- if($baiarr[2]==$yearr[2]){$wtext[$i].=$baiarr[2];}else{$wtext[$i].=str_replace("风","",$baiarr[2]."转".$yearr[2]);$wtext[$i].="风";}//将风向添加到返回值里
- if($baiarr[3]!="微风"){$wtext[$i].=$baiarr[3];}//将风力添加到返回值
- }
- return $text.implode("",$wtext);
- }else{
- return "无法获取到该城市的天气信息!";
- }
- }
- function wchangearray($arr)//对数组进行键值排序
- {
- foreach($arr as $v)
- {if(!trim($v))continue;
- $value[]=trim($v);}return $value;
- }
|