Discuz教程网

php获取各城市72小时天气预报[中国天气网]

[复制链接]
authicon dly 发表于 2013-2-21 22:37:30 | 显示全部楼层 |阅读模式
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
  1. function getweather($city,$data='data.txt')//获取天气预报内容
  2. {
  3.     $urlarr=unserialize(file_get_contents($data));
  4.     if($urlarr[$city])
  5.     {
  6.         $url = $urlarr[$city];$text=$city;
  7.         $lines_string = file_get_contents($url);
  8.         $lines_string = explode("<!--day",$lines_string);
  9.         if(!$lines_string[1]){return "无法获取到该城市的天气信息!";exit;}
  10.         $lines_string_3=explode("未来",$lines_string[3]);
  11.         $lines_array =array(str_replace('1-->','',$lines_string[1]),str_replace('2-->','',$lines_string[2]),str_replace('3-->','',$lines_string_3[0]));
  12.         for($i=0;$i< count($lines_array); $i ++)
  13.         {
  14.             $tiqian=array("℃","高温","低温");$tihou=array("度","","");$nowarray=str_replace($tiqian,$tihou,strip_tags($lines_array[$i]));
  15.             $datearray=explode("日",$nowarray);$wtext[$i]=trim($datearray[0])."日";//获取日期
  16.             $weather=explode("白天",$nowarray);$weather=explode("夜间",$weather[1]);
  17.             $baiarr=wchangearray(explode("r",$weather[0]));//白天天气
  18.             $yearr=wchangearray(explode("r",$weather[1]));//夜间天气
  19.             if($baiarr[0]==$yearr[0]){$wtext[$i].=$baiarr[0];}else{$wtext[$i].=$baiarr[0]."转".$yearr[0];}//将天气添加到返回值里
  20.             $wtext[$i].=$baiarr[1]."到".$yearr[1];//将气温添加到返回值里
  21.             if($baiarr[2]==$yearr[2]){$wtext[$i].=$baiarr[2];}else{$wtext[$i].=str_replace("风","",$baiarr[2]."转".$yearr[2]);$wtext[$i].="风";}//将风向添加到返回值里
  22.             if($baiarr[3]!="微风"){$wtext[$i].=$baiarr[3];}//将风力添加到返回值
  23.         }
  24.         return $text.implode("",$wtext);
  25.     }else{
  26.         return "无法获取到该城市的天气信息!";
  27.     }
  28. }

  29. function wchangearray($arr)//对数组进行键值排序
  30. {
  31.         foreach($arr as $v)
  32.         {if(!trim($v))continue;
  33.         $value[]=trim($v);}return $value;
  34. }
复制代码





上一篇:中国天气网的中央气象台实时数据接口
下一篇:php抓取天气预报(天气,气温,风力,日出日落...)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 07:18

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表