:::

即時空氣品質及天氣


套件名稱: 即時空氣品質及天氣 1
作者 : Cyujin Sai
模組網站 :

即時空氣品質及天氣 更新說明

首次釋出,2019-11-29 修正連線問題

「 即時空氣品質及天氣 」簡介

Cyujin Sai 老師開發的即時空氣品質及天氣區塊

安裝方式

  1. 到區塊管理,新增一個自訂區塊。
  2. 將底下內容貼到自訂區塊中
    $showcity = '彰化市';		//設定欲顯示之中文城市名稱!若不填或將本行註解掉,則依下方設定來顯示!
    
    //主要AQI資料來源參數:若不使用epa資料,請將 $sitename 行註解掉,或留空值!(注意!主要AQI資料來源早上7:30~8:30之間可能會斷線)
    $url = 'https://opendata.epa.gov.tw/api/v1/AQI/?skip=0&top=1000&format=json';
    $link = 'https://taqm.epa.gov.tw/taqm/tw/';
    //$sitename = '彰化';	//設定要擷取資料的站台,請參考上方 $url 網頁資料 SiteName 欄位!若錯誤,會使用 aqicn 數據!
    //$sitename = '屏東(琉球)';
    
    //主要氣溫查詢參數:如果不要顯示當地氣溫,請將 $tempid 留空,或將下行註解掉!
    $tempid = '141';	//可用 Firefox 瀏覽器至 https://works.ioa.tw/weather/api/all.json 查詢代碼
    //以下詳細天氣連結,請二選一
    //$templink = 'https://works.ioa.tw/weather/maps.html';			//ioa 天氣地圖連結
    $templink = 'https://www.cwb.gov.tw/m/f/town368/1000701.php';	//氣象局鄉鎮預報連結,請搜尋後修改網址
    
    //次要AQI資料來源參數:
    $cityid = '1618';		//若有設定$city,此處可以不填,但建議填寫! 
    $city = 'changhua';		//若有設定$cityid,此處可以不填,但建議填寫!
    $token = '32f529a1fc2b16476338548abb4f281f22509d94';
    //可沿用此Token (Air Quality Open Data Platform API Token),或至 https://aqicn.org/data-platform/token/ 申請
    //欲更改城市位置,可至 https://campus-xoops.tn.edu.tw/modules/tad_modules/index.php?module_sn=100 查看縣市鄉鎮的英文名稱,並於上方填入城市代號或英文名稱!
    
    //AIRQ 在地空氣品質查詢參數:如果不要顯示AIRQ在地空氣品質,請將 $airqn 留空,也可將 $airq 或 $airqn 註解掉!
    $airq = "http://www.airq.org.tw/Home/realtime10m";		//AIRQ 官網提供資料來源
    $airqn = "東芳國小";		//AIRQ 監測點名稱,可至 http://www.airq.org.tw/Home/Value 查詢!
    $airql = "http://www.airq.org.tw/Home/Value";		//AIRQ 官網連結
    
    //AQI 各等級版面預覽測試
    //$test_aqiv = '123';	 //只有測試時才填入欲顯示之 AQI 值,平時請設定為空值 或 將本行註解掉!
    //等級範圍:1.良好 0~50 2.普通 51~100 3.輕度污染 101~150 4.中度污染 151~200
    //     5.重度污染 201~300      6.嚴重污染 301~500
    
    // get_aqi_json 函數
    if (!function_exists('get_aqi_json')) {
        function get_aqi_json($url)
        {
            $data = '';
            if (function_exists('curl_init')) {
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
                curl_setopt($ch, CURLOPT_HEADER, false);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
                curl_setopt($ch, CURLOPT_URL, $url);
                //curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0");
                //curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36');
                curl_setopt($ch, CURLOPT_REFERER, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
                curl_setopt($ch, CURLOPT_TIMEOUT, 2);
                $data = curl_exec($ch);
                curl_close($ch);
            } else {
                $arrContextOptions = array(
                    "ssl" => array(
                        "verify_peer"      => false,
                        "verify_peer_name" => false,
                    ),
                );
                $data = file_get_contents($url, false, stream_context_create($arrContextOptions));
            }
            return $data;
        }
    }
    
    //主要AQI資料來源
    if (!empty($sitename)) {
        $data = get_aqi_json($url);
        if (!empty($data)) {
            $json = json_decode($data, 1);
            $data = array_column($json, null, 'SiteName');
            $aqiv = $data[$sitename]['AQI'];					//AQI值
            $pm25 = $data[$sitename]['PM2.5'];					//PM2.5值
            $windspeed = $data[$sitename]['WindSpeed'];			//風速
            $winddirec = $data[$sitename]['WindDirec'];			//風向
            $utime = strtotime($data[$sitename]['PublishTime']);		//時間戳~數據update時間
            $udate = date('Ymd',$utime);						//數據update日期
            $utime = date('Y年n月j日 G時i分', $utime);				//數據update時間
            if (empty($showcity)&&!empty($aqiv)) {
              if (strpos($sitename,"(")) {
                $showcity = substr($sitename, strpos($sitename,"(")+1, strpos($sitename,")")-strpos($sitename,"(")-1 );
              } else {
                $showcity = $sitename;
              }
            }
            if (is_numeric($winddirec)) {
              //$winde = array("N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW");
              //$windc = array("北", "北北東", "東北", "東北東", "東", "東南東", "東南", "南南東", "南", "南南西", "西南", "西南西", "西", "西北西", "西北", "北北西");
              $windc = array("北", "偏北", "東北", "偏東", "東", "偏東", "東南", "偏南", "南", "偏南", "西南", "偏西", "西", "偏西", "西北", "偏北");
              $winds = array("↓", "↓", "↙", "←", "←", "←", "↖", "↑", "↑", "↑", "↗", "→", "→", "→", "↘", "↓");
              $windr = (ceil(($winddirec/22.5)+0.5)-1)%16;
              //$winde = $winde[$windr];
              //$winde = "<img border=0 src=https://www.cwb.gov.tw/m/assets/images/wind/{$winde}.png>";
              $windc = $windc[$windr];
              $winds = $winds[$windr];
            } 
            if (!empty($city)) {$cityname = ucfirst($city);}
            $detail = $link;
        }
    }
    
    //次要AQI資料來源
    if (empty($aqiv)) {
        if (empty($cityid)) {
            $url = "http://api.waqi.info/search/?token={$token}&keyword={$city}";
            $data = get_aqi_json($url);
            if (!empty($data)) {
                $json = json_decode($data, 1);
                $cityid = $json['data'][0]['uid'];
            } else {
                echo "<a href='http://aqicn.org/city/taiwan/$city/' target='_blank'>抓不到 $city 的城市代號資料,請稍後重試</a>";
            }
        }
    
        if (!empty($cityid)&&empty($aqiv)) {
            //$url1 = "https://api.waqi.info/api/feed/@{$cityid}/obs.en.json";
            $url1 = "https://api.waqi.info/feed/@{$cityid}/?token={$token}";
            $data1 = get_aqi_json($url1);
            if (!empty($data1)) {
                $aqi = json_decode($data1, 1);
                $cityname = $aqi['data']['city']['name'];		//城市名
                $cityname = substr($cityname, 0, strpos($cityname,','));
                $cityname = ucfirst($cityname);
                $detail = $aqi['data']['city']['url'];			//詳細資料連結網址
                $pm25 = $aqi['data']['iaqi']['pm25']['v'];		//PM2.5
                //$aqiv = $aqi['data']['aqi'];				//AQI值
                if (!empty($tempid)&&empty($tempv)) {
                    $tempv = $aqi['data']['iaqi']['t']['v'];		//溫度
                    if (is_numeric($tempv)) {
                        $tempv = round($tempv,1);
                        $weather = " <span style='font-size: 0.8em;'>{$tempv}<sup>°c</sup></span>";
                    }
                }
                if (empty($showcity)) {
                    if (!empty($sitename)) {
                       if (strpos($sitename,"(")) {
                         $showcity = substr($sitename, strpos($sitename,"(")+1, strpos($sitename,")")-strpos($sitename,"(")-1 );
                       } else {
                         $showcity = $sitename;
                       }
                    } else {
                       $showcity = $cityname;
                    }
                }
                $microtime = microtime(true) * 1000;
                $url2 = "https://alpha.waqi.info/xservices/refresh:{$cityid}?_" . $microtime;
                $data2 = get_aqi_json($url2);
                if (!empty($data2)) {
                    $now = json_decode($data2, 1);
                    $aqiv = $now['aqiv'];				//AQI值
                    $utime = $now['mtime'];			//時間戳~數據update時間
                    $udate = date('Ymd',$utime);		//數據update日期
                    //$utime = date('Y年m月d日 H時i分', $utime);
                    $utime = date('Y年n月j日 G時i分', $utime);
                } else {
                    echo "<a href='http://aqicn.org/city/taiwan/$city/' target='_blank'>抓不到 $city 的空氣資料,請稍後重試,或點此連結觀看原始網站資料</a>";
                }
            } else {
                echo "<a href='http://aqicn.org/city/taiwan/$city/' target='_blank'>抓不到指定城市 $city 的資料,請稍後重試,或點此連到原始網站觀看資料</a>";
            }
        }
    }
    
    //主要氣溫查詢
    if (!empty($tempid)) {
        $url = "https://works.ioa.tw/weather/api/weathers/{$tempid}.json";
        $data = get_aqi_json($url);
        if (!empty($data)) {
            $json = json_decode($data, 1);
            if (is_numeric($json['temperature'])) {
              $tempv = round($json['temperature'],1);		//溫度
            }
            $temph = strtotime($json['at']);				//更新時間
            $desc = $json['desc'];					//天氣敘述
            $tempf = round($json['felt_air_temp'],1);		//體感溫度
            $humidity = $json['humidity'];				//濕度
            $rainfall = $json['rainfall'];					//雨量
            $sunrise = $json['sunrise'];				//日出時間
            $sunset = $json['sunset'];					//日落時間
            $weather = $json['img'];					//天氣圖檔名稱
            if ($udate == date('Ymd',$temph)) {
            $temph = date('G時i分', $temph);
            } else {
            $temph = date('n月j日G時i分', $temph);
            }
            $desc = "【天氣概況】
     {$desc}
    當前溫度:{$tempv}°c
    體感溫度:{$tempf}°c
    當前濕度:{$humidity} %
    當前雨量:{$rainfall} mm
    當前風速:{$windspeed} m/sec
    當前風向:{$winds}{$windc}風 {$winddirec}°
    日出時間:{$sunrise}
    日落時間:{$sunset}
    
    天氣數據於 {$temph} 更新";
            $weather = "<img src='https://works.ioa.tw/weather/img/weathers/zeusdesign/{$weather}' style='height: 1.6em;' border='0'>";
        }
        if (is_numeric($tempv)) {
            $tempv = round($tempv,1);
            $weather = "<a target='_blank' href='{$templink}' title='$desc' style='color: white;' onMouseOver=\"this.style.backgroundColor='#0F0'\" onMouseOut=\"this.style.backgroundColor=''\">{$weather}<span style='font-size: 0.8em;' title='$desc'>{$tempv}<sup>°c</sup></span></a>";
        }
    }
    
    //AIRQ 在地空氣品質查詢
    if (!empty($airq)&&!empty($airqn)) {
        $data = get_aqi_json($airq);
        if (!empty($data)) {
            $json = json_decode($data, 1);
            $data = array_column($json, null, 'Name');
            $airqv = $data[$airqn]['Value'];				//PM2.5 十分鐘平均數值
            $airqt = $data[$airqn]['EndTime'];			//產出數據時間
            $airqt = substr($airqt, strpos($airqt," "), strlen($airqt));
            if (is_numeric($airqv)) {
              $airqv = round($airqv,1);
              if (is_numeric($pm25)) {
                $pm25 = round($pm25,1);
                $pm25 = "
    
    ~PM2.5資料對比~
      {$showcity} :{$pm25}
      {$airqn}:{$airqv}";
              } else {
                $pm25 = "";
              }
              $airqv = "<span style='font-size: 0.8em;' title='【PM2.5 微型感測器】
      {$airqt}
      十分鐘平均數值{$pm25}'><a target='_blank' href='{$airql}' style='color: white;' onMouseOver=\"this.style.color='#00F'\" onMouseOut=\"this.style.color='#FFF'\">{$airqn}PM2.5平均:{$airqv}</a>";
            }else{
              $airqv = "<span style='font-size: 0.8em;' title='目前沒有 {$airqn} PM2.5 資料,
    請按此查看即時數值!'><a target='_blank' href='{$airql}' style='color: white;' onMouseOver=\"this.style.color='#00F'\" onMouseOut=\"this.style.color='#FFF'\">PM2.5 微型感測器</a>";
            }	
        }
    }
    
    if ($test_aqiv) {
        $aqiv = $test_aqiv;
        $showcity = "測試!";
    }
    
    if (!empty($aqiv)) {
        if ($aqiv <= 50) {
            $n       = 1;
            $rank    = "良好";
            $range    = "0~50:良好 Good";
            $desc    = "????空氣質量令人滿意,基本無空氣污染";
            $suggest = "????各類人群可正常活動";
            $color   = '#009966';
            $color2  = 'rgba(0, 153, 102, 0.1)';
        } elseif ($aqiv <= 100) {
            $n       = 2;
            $rank    = "普通";
            $range    = "51~100:普通 Moderate";
            $desc    = "????空氣質量可接受,但某些污染物可能對極少數異常敏感人群健康有較弱影響";
            $suggest = "????極少數異常敏感人群應減少戶外活動";
            $color   = '#FFCC00';
            $color2  = 'rgba(255, 222, 51, 0.1)';
        } elseif ($aqiv <= 150) {
            $n       = 3;
            $rank    = "<span style='font-size: 0.7em;'>輕度污染</span>";
            $range    = "101~150:輕度污染 Unhealthy for Sensitive Groups";
            $desc    = "????易感人群症狀有輕度加劇,健康人群出現刺激症狀";
            $suggest = "????<span style='color: #FF0000;'>對敏感族群不健康!</span><br>兒童、老年人及心臟病、呼吸系統疾病患者應減少長時間、高強度的戶外鍛鍊";
            $color   = '#FF9933';
            $color2  = 'rgba(255, 153, 51, 0.1)';
        } elseif ($aqiv <= 200) {
            $n       = 4;
            $rank    = "<span style='font-size: 0.7em;'>中度污染</span>";
            $range    = "151~200:中度污染 Unhealthy";
            $desc    = "????進一步加劇易感人群症狀,可能對健康人群心臟、呼吸系統有影響";
            $suggest = "????<span style='color: #FF0000;'>對所有族群不健康!</span><br>兒童、老年人及心臟病、呼吸系統疾病患者避免長時間、高強度的戶外鍛鍊,一般人群適量減少戶外運動";
            $color   = '#CC0033';
            $color2  = 'rgba(204, 0, 51, 0.1)';
        } elseif ($aqiv <= 300) {
            $n       = 5;
            $rank    = "<span style='font-size: 0.7em;'>重度污染</span>";
            $range    = "201~300:重度污染 Very Unhealthy";
            $desc    = "????心臟病和肺病患者症狀顯著加劇,運動耐受力降低,健康人群普遍出現症狀";
            $suggest = "????<span style='color: #FF0000;'>非常不健康!</span><br>兒童、老年人及心臟病、肺病患者應停留在室內,停止戶外運動,一般人群減少戶外運動";
            $color   = '#660099';
            $color2  = 'rgba(102, 0, 153, 0.1)';
        } else {
            $n       = 6;
            $rank    = "<span style='font-size: 0.7em;'>嚴重污染</span>";
            $range    = "301~500:嚴重污染 Hazardous";
            $desc    = "????健康人群運動耐受力降低,有明顯強烈症狀,提前出現某些疾病";
            $suggest = "????<span style='color: #FF0000;'>危害!</span>兒童、老年人和病人應停留在室內,避免體力消耗,一般人群避免戶外活動";
            $color   = '#7E0023';
            $color2  = 'rgba(126, 0, 35, 0.1)';
        }
     
             $widget = "
             <div style='border: 1px solid $color; border-radius: 6px; width: 100%; background: $color; padding: 0x; '>
    
         <div style=\"font-family: 'Microsoft JhengHei','Segoe UI',Arial,Verdana,fantasy; margin: 4px 4px; text-align:center;\">
            <div style='font-size: 1.4em; font-weight: bold; color: white;'><span title='Real-time Air Quality'>即時空氣品質</span><br><a href='{$detail}' target='_blank' title='按此查看 {$showcity} 即時空氣品質詳細資料' onMouseOver=\"this.style.backgroundColor='#0F0'\" onMouseOut=\"this.style.backgroundColor=''\" 
    '>{$showcity}</a>{$weather}</div>
            <div style='font-size: 0.8em; color: white;' title='資料更新時間 Updated Time'>{$utime}</div>
         </div>
     
         <div style=\"font-family: 'Microsoft JhengHei','Segoe UI',Arial,Verdana,fantasy; margin: 4px 4px; padding: 8px 4px 4px; background-color: white; position: relative; overflow: hidden; border-radius: 6px;\">
               <div style='font-size: 1.8em; font-weight: bold; text-align:center;' title='AQI 值 {$range}';>
                <span style='font-family: Verdana; background-color: {$color}; color: white; padding: 2px 4px; text-shadow: rgb(3, 3, 3) 1px 1px 1px; border-radius: 6px;'>{$aqiv}</span>
                <span style='color: {$color};'>{$rank}</span>
            </div>
             
            <div style=\"font-family: 'Microsoft JhengHei','Segoe UI',Arial,Verdana,fantasy; margin: 4px 4px 4px 4px; font-size:0.9em;\">
                <div style=\"margin: 4px 0px;\">{$desc}</div>
                <div style=\"margin: 4px 0px;\">{$suggest}</div>
            </div>
            <img src='https://campus-xoops.tn.edu.tw/uploads/aqi_{$n}.svg' style='width:80px; height:80px; position: absolute; bottom: 6px; right:40px;'>
                </div>
             <div style=\"font-family: 'Segoe UI',Arial,Verdana,fantasy; margin: 4px 4px 4px 4px; font-size:14px;\">
            </div>
            <div style='color: white; text-align:center;'>{$airqv}</div>
            </div>";
     
        echo $widget;
    }
  3. 內容類型請設為PHP腳本
  4. 位置建議放在 左邊

即時空氣品質及天氣 更新項目

即時空氣品質及天氣 1 (2019-11-29 16:00:00 起被下載 1011 次)

首次釋出

2019-11-29 修正連線問題

:::

快速登入


http%3A%2F%2Fcampus-xoops.tn.edu.tw%2Fmodules%2Ftad_modules%2Findex.php%3Fmodule_sn%3D112

計數器

今天: 3123312331233123
昨天: 2489248924892489
總計: 8025105802510580251058025105802510580251058025105