国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > PHP > 正文

php采集天氣預(yù)報(bào)代碼

2024-05-04 21:46:58
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
<?php
/**
* 采集天氣預(yù)報(bào)
* @example
*   weather::$cache = root.'chache/'; //如果不改緩存目錄,可以不寫(xiě)
*  $array = weather::get();
* @author lrenwang
* @e-mail [email protected]
*
*/
class weather{
  /**
   * 域名
   *
   * @var string
   */
  static public $domain='http://qq.ip138.com';
  /**
   * 城市的連接
   * @example /省份/城市.htm
   *
   * @var string
   */
  static public $url='/weather/hebei/qinhuangdao.htm';
  /**
   * 緩存目錄
   *
   * @var string
   */
  static public $cache='cache/';
  /**
   * 更新頻率,小時(shí)為單位
   *
   * @var unknown_type
   */
  static public $h=1;
  
  /**
   * 天氣預(yù)報(bào)對(duì)應(yīng)的圖片路徑
   *
   * @var unknown_type
   */
  static public $img = 'images/weather/';

  /**
   * 獲得今日,明日天氣預(yù)報(bào)
   *
   * @return array
   */
  
  static function get()
  {
    $time=time();
    
    //緩存文件位置
    $cache_file = self::$cache.'weather.dat';
    //判斷緩存文件是否存在,沒(méi)有則生成
    if(!is_file($cache_file)){
      if(!is_dir(self::$cache))
      mkdir(self::$cache,0777,true);
      self::get_($cache_file);
    }
    
    //判斷緩存是否過(guò)期,過(guò)期則重新生成
    if($time-filemtime($cache_file)>=3600*3){
      self::get_($cache_file);
    }
    $arr=unserialize(file_get_contents($cache_file));
    return $arr;
  }
  
  /**
   * 獲得緩存
   *
   * @param unknown_type $cache_file
   */
  static public function get_($cache_file)
  {
    $con = file_get_contents(self::$domain.self::$url);
    preg_match('~<table width="700" borderColorDark="#ffffff".*?>(.*?)</table>~s',$con,$table);
    preg_match_all('~<tr.*?>(.*?)</tr>~s',$table[1],$trs);
    $i=0;
    $array = array();

    foreach ($trs[1] as $tr)
    {
      ++$i;
      preg_match_all('~<t[dh].*?>(.*?)</t[dh]>~s',$tr,$tds);
      $array[0][] = self::I($tds[1][1]);
      $array[1][] = self::I($tds[1][2]);
    }
    $array[0][1] = self::get_img($array[0][1]);
    $array[1][1] = self::get_img($array[1][1]);
    file_put_contents($cache_file,serialize($array));
  }

  /**
   * 數(shù)據(jù)輸出測(cè)試
   *
   * @param unknown_type $data
   * @param unknown_type $s
   */
  static public function P($data,$s=0)
  {
    echo "<pre>";
    if (is_array($data))
    var_export($data);
    else
    echo $data;
    echo '</pre>';
    if ($s==0)
    exit();
  }
  /**
   * 轉(zhuǎn)碼
   *
   * @param unknown_type $str
   * @return unknown
   */
  static public function I($str)
  {
    return iconv('GB2312','UTF-8',$str);
  }

  /**
   * 獲得圖片的URL,此處可擴(kuò)展,把圖片抓取到本地, 增加訪問(wèn)速度
   *
   * @param string $str
   * @return string
   */
  static public function get_img($str)
  {
    preg_match('~src=["/']?(.*?)["/']? ~s',$str,$p);
    return self::$img.pathinfo($p[1],PATHINFO_BASENAME);
  }
}
//print_r();
$weather = weather::get();
echo "<img src={$weather[0][1]}>";
?>



發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 句容市| 大兴区| 新竹县| 大石桥市| 清流县| 清水河县| 苗栗县| 镇原县| 阳东县| 依安县| 赤水市| 永胜县| 闻喜县| 冷水江市| 海伦市| 霍林郭勒市| 通江县| 平遥县| 新巴尔虎左旗| 五华县| 马关县| 莆田市| 珲春市| 富蕴县| 门头沟区| 奉新县| 渑池县| 拉孜县| 伊川县| 衡东县| 罗田县| 三门峡市| 汝州市| 漳平市| 河东区| 荆门市| 小金县| 库尔勒市| 故城县| 常德市| 高州市|