主要是解決內容一直停留在 2019/05/11 的問題
台灣即時空氣品質(AQI),有相關空氣資料的監測點如下(儘量已編號為主,較為準確):
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1651
1654
1596
1597
1598
1599
1600
1601
1648
1602
1603
1604
1605
1606
1652
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1653
1656
1622
1623
1626
1661
1624
1625
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1655
1643
1644
1645
1649
1650
1646
1660
1647
1658
1659
1657
8768
$cityid = '1631'; $city = 'tainan'; //若有上方代號,此處可不填 //欲更改城市位置,可至 https://www.tad0616.net/modules/tad_modules/index.php?module_sn=100 查看縣市鄉鎮的英文名稱,並於上方填入城市代號(必填)及英文名稱(選填)即可。 //以下請勿動 if (empty($cityid)) { $url = 'http://api.waqi.info/search/?token=32f529a1fc2b16476338548abb4f281f22509d94&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 (!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_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; } } if (!empty($cityid)) { $url1 = "https://api.waqi.info/api/feed/@{$cityid}/obs.en.json"; $data1 = get_aqi_json($url1); if (!empty($data1)) { $aqi = json_decode($data1, 1); $cityname = $aqi['rxs']['obs'][0]['msg']['city']['name']; $detail = $aqi['rxs']['obs'][0]['msg']['city']['url']; $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']; $utime = date('Y年m月d日 H時i分', $now['mtime']); if ($aqiv <= 50) { $n = 1; $rank = "優"; $desc = "空氣質量令人滿意,基本無空氣污染"; $suggest = "各類人群可正常活動"; $color = '#009966'; $color2 = 'rgba(0, 153, 102, 0.1)'; } elseif ($aqiv <= 100) { $n = 2; $rank = "良"; $desc = "空氣質量可接受,但某些污染物可能對極少數異常敏感人群健康有較弱影響"; $suggest = "極少數異常敏感人群應減少戶外活動"; $color = '#FFDE33'; $color2 = 'rgba(255, 222, 51, 0.1)'; } elseif ($aqiv <= 150) { $n = 3; $rank = "輕度污染"; $desc = "易感人群症狀有輕度加劇,健康人群出現刺激症狀"; $suggest = "兒童、老年人及心臟病、呼吸系統疾病患者應減少長時間、高強度的戶外鍛鍊"; $color = '#FF9933'; $color2 = 'rgba(255, 153, 51, 0.1)'; } elseif ($aqiv <= 200) { $n = 4; $rank = "中度污染"; $desc = "進一步加劇易感人群症狀,可能對健康人群心臟、呼吸系統有影響"; $suggest = "兒童、老年人及心臟病、呼吸系統疾病患者避免長時間、高強度的戶外鍛鍊,一般人群適量減少戶外運動"; $color = '#CC0033'; $color2 = 'rgba(204, 0, 51, 0.1)'; } elseif ($aqiv <= 300) { $n = 5; $rank = "重度污染"; $desc = "心臟病和肺病患者症狀顯著加劇,運動耐受力降低,健康人群普遍出現症狀"; $suggest = "兒童、老年人及心臟病、肺病患者應停留在室內,停止戶外運動,一般人群減少戶外運動"; $color = '#660099'; $color2 = 'rgba(102, 0, 153, 0.1)'; } else { $n = 6; $rank = "嚴重污染"; $desc = "健康人群運動耐受力降低,有明顯強烈症狀,提前出現某些疾病"; $suggest = "兒童、老年人和病人應停留在室內,避免體力消耗,一般人群避免戶外活動"; $color = '#7E0023'; $color2 = 'rgba(126, 0, 35, 0.1)'; } $widget = " <div style='border: 1px solid $color; border-radius: 6px; width: 100%; background: $color2; padding: 0x; '> <div style=\"font-family: 'Microsoft JhengHei','Segoe UI',Arial,Verdana,fantasy; margin: 10px 15px;\"> <div style='font-size: 1.3em'; font-weight: bold;><a href='{$detail}' target='_blank'>{$cityname}</a>的即時空氣品質</div> <div style='font-size: 0.8em';>{$utime}</div> </div> <div style=\"font-family: 'Microsoft JhengHei','Segoe UI',Arial,Verdana,fantasy; margin: 10px 10px; padding: 6px 15px; background-color: white; position: relative; overflow: hidden;\"> <div style='font-size: 2.2em;'> <span style='font-family: Verdana; background-color: {$color}; color: white; border-radius: 6px;padding: 2px 10px;text-shadow: rgb(3, 3, 3) 1px 1px 1px;'>{$aqiv}</span> <span style='color: {$color};'>{$rank}</span> </div> <div style=\"font-family: 'Segoe UI',Arial,Verdana,fantasy; margin: 10px 15px 0px; font-size:12px;\"> <div>{$desc}</div> <div>{$suggest}</div> </div> <img src='https://campus-xoops.tn.edu.tw/uploads/aqi_{$n}.svg' style='width:160px; height:160px; position: absolute; top:-28px; right:-30px;'> </div> </div>"; echo $widget; } 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>"; } } else { echo "<a href='http://aqicn.org/city/taiwan/$city/' target='_blank'>抓不到指定城市 $city 的城市代號資料,請稍後重試,或點此連到原始網站觀看資料</a>"; }
PHP腳本
左邊
主要是解決內容一直停留在 2019/05/11 的問題
重新改寫,以支援台灣各地點。
欲更改城市位置,可至 https://www.tad0616.net/modules/tad_modules/index.php?module_sn=100 查看縣市鄉鎮的英文名稱,並於上方填入英文名稱即可。
正式釋出