欲更改城市位置,可至 https://www.tad0616.net/modules/tad_modules/index.php?module_sn=100 查看縣市鄉鎮的英文名稱,並於上方填入英文名稱即可。
台灣即時空氣品質(AQI),有相關空氣資料的監測點如下(儘量已編號為主,較為準確):
15861587158815891590159115921593159415951651165415961597159815991600160116481602160316041605160616521607160816091610161116121613161416151616161716181619162016211653165616221623162616611624162516271628162916301631163216331634163516361637163816391640164116421655164316441645164916501646166016471658165916578768$city = 'tainan';
//欲更改城市位置,可至 https://www.tad0616.net/modules/tad_modules/index.php?module_sn=100 查看縣市鄉鎮的英文名稱,並於上方填入英文名稱即可。
//以下請勿動
$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'];
    $url   = "https://api.waqi.info/api/feed/@{$json['data'][0]['uid']}/obs.hk.json";
    $data1 = get_aqi_json($url);
    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;
        $url       = "http://feed.aqicn.org/xservices/refresh:{$cityid}?_" . $microtime;
        $data2     = get_aqi_json($url);
        if (!empty($data2)) {
            $now = json_decode($data2, 1);
            $aqiv  = $now['aqiv'];
            $utime = date('Y年m月d日 H時i分', $now['mtime']);
            if ($aqiv <= 50) {
                $rank    = "優";
                $desc    = "空氣質量令人滿意,基本無空氣污染";
                $suggest = "各類人群可正常活動";
                $color   = '#009966';
                $color2  = 'rgba(0, 153, 102, 0.1)';
            } elseif ($aqiv <= 100) {
                $rank    = "良";
                $desc    = "空氣質量可接受,但某些污染物可能對極少數異常敏感人群健康有較弱影響";
                $suggest = "極少數異常敏感人群應減少戶外活動";
                $color   = '#FFDE33';
                $color2  = 'rgba(255, 222, 51, 0.1)';
            } elseif ($aqiv <= 150) {
                $rank    = "輕度污染";
                $desc    = "易感人群症狀有輕度加劇,健康人群出現刺激症狀";
                $suggest = "兒童、老年人及心臟病、呼吸系統疾病患者應減少長時間、高強度的戶外鍛鍊";
                $color   = '#FF9933';
                $color2  = 'rgba(255, 153, 51, 0.1)';
            } elseif ($aqiv <= 200) {
                $rank    = "中度污染";
                $desc    = "進一步加劇易感人群症狀,可能對健康人群心臟、呼吸系統有影響";
                $suggest = "兒童、老年人及心臟病、呼吸系統疾病患者避免長時間、高強度的戶外鍛鍊,一般人群適量減少戶外運動";
                $color   = '#CC0033';
                $color2  = 'rgba(204, 0, 51, 0.1)';
            } elseif ($aqiv <= 300) {
                $rank    = "重度污染";
                $desc    = "心臟病和肺病患者症狀顯著加劇,運動耐受力降低,健康人群普遍出現症狀";
                $suggest = "兒童、老年人及心臟病、肺病患者應停留在室內,停止戶外運動,一般人群減少戶外運動";
                $color   = '#660099';
                $color2  = 'rgba(102, 0, 153, 0.1)';
            } else {
                $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-color: $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 0px;padding: 6px 15px; background-color: white;\">
                    <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>
                <div style=\"font-family: 'Segoe UI',Arial,Verdana,fantasy; margin: 10px 15px; font-size:12px;\">
                    <div>{$desc}</div>
                    <div>{$suggest}</div>
                </div>
            </div>";
            echo $widget;
        } else {
            echo "<a href='$url' target='_blank'>抓不到指定城市 $city 的空氣資料,請稍後重試</a>";
        }
    } else {
        echo "<a href='$url' target='_blank'>抓不到指定城市 $city 的資料,請稍後重試</a>";
    }
} else {
    echo "<a href='$url' target='_blank'>抓不到指定城市 $city 的城市代號資料,請稍後重試</a>";
}
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);
        $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;
}
PHP腳本
                左邊
                            重新改寫,以支援台灣各地點。
欲更改城市位置,可至 https://www.tad0616.net/modules/tad_modules/index.php?module_sn=100 查看縣市鄉鎮的英文名稱,並於上方填入英文名稱即可。
正式釋出