国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 敦煌市| 奈曼旗| 忻州市| 广南县| 秭归县| 叶城县| 大方县| 思南县| 富阳市| 石嘴山市| 咸丰县| 库车县| 上饶县| 宜昌市| 巧家县| 沾益县| 鹤山市| 平阳县| 北京市| 花莲市| 炎陵县| 陕西省| 邵阳市| 东乌| 长白| 昌吉市| 平乐县| 常德市| 迁安市| 策勒县| 婺源县| 中山市| 牙克石市| 西青区| 开阳县| 如东县| 四子王旗| 工布江达县| 湖南省| 濉溪县| 衡阳县|