正式釋出
此區塊可以擷取台北市教育局公告的json檔,並即時呈現公告。
其中 $limit 可以自訂呈現篇數
若有不需要的項目,請自行註記 $url_arr 即可(在最左邊加上//)。
$limit = 5; //不需要的公告,請自行註解掉(在最左邊加上//) $url_arr['一般公告'] = 'https://www.doe.gov.taipei/OpenData.aspx?SN=A53E0BA10DD4457E'; $url_arr['幼兒園'] = 'https://www.doe.gov.taipei/OpenData.aspx?SN=D5AD2C5A7A85502C'; $url_arr['國小'] = 'https://www.doe.gov.taipei/OpenData.aspx?SN=A06275FDC88942AB'; $url_arr['國中'] = 'https://www.doe.gov.taipei/OpenData.aspx?SN=2376933CA35541CB'; $url_arr['高中職'] = 'https://www.doe.gov.taipei/OpenData.aspx?SN=8FDEB38F0F17B561'; $url_arr['終身教育'] = 'https://www.doe.gov.taipei/OpenData.aspx?SN=711FA93AC6D09A71'; if (function_exists('curl_init')) { if (!file_exists(XOOPS_ROOT_PATH . "/modules/tadtools/easy_responsive_tabs.php")) { redirect_header("index.php", 3, '需要最新版TadTools'); } include_once XOOPS_ROOT_PATH . "/modules/tadtools/easy_responsive_tabs.php"; $responsive_tabs = new easy_responsive_tabs('#tpNewsTab'); $responsive_code = $responsive_tabs->rander(); $tabs = $tab_content = $msg = ''; foreach ($url_arr as $tab_title => $url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2); $data = curl_exec($ch); if ($data === false) { $msg .= '<div>Curl 錯誤:' . curl_error($ch).'</div>'; } else { curl_close($ch); $tabs .= "<li>$tab_title</li>"; // echo $data; if ($data) { if (substr($data, 0, 3) == pack("CCC", 0xef, 0xbb, 0xbf)) { $data = substr($data, 3); } $news_arr = json_decode($data, true); // echo json_last_error() . "\n"; // 4 // echo json_last_error_msg() . "\n"; // Syntax error, malformed JSON $all_news = array(); $i = 0; if ($news_arr) { foreach ($news_arr as $news) { if (isset($news['發布日期']) and isset($news['Source']) and isset($news['title']) and isset($news['發布單位'])) { $date = $news['發布日期']; $all_news[$date]['link'] = $news['Source']; $all_news[$date]['title'] = $news['title']; $all_news[$date]['unit'] = $news['發布單位']; } $i++; } krsort($all_news); $i = 0; $tab_content .= " <div> <table class='table table-bordered table-condensed table-hover table-striped'> <tr> <th>標題</th> <th>發布日期</th> <th>發布機關</th> </tr>"; foreach ($all_news as $date => $news) { $date = substr($date, 0, 10); $tab_content .= " <tr> <td>{$date}</td> <td><a href='{$news['link']}' target='_blank'>{$news['title']}</a></td> <td>{$news['unit']}</td> </tr>"; $i++; if ($i >= $limit) { break; } } $tab_content .= " </table> </div>"; } else { var_export($news_arr); $msg .= "<div>無法解析 {$url} 的資料</div>"; } } else { $msg .= "<div>抓不到 {$url} 的資料</div>"; } } } if ($msg) { echo $msg; } else { echo $responsive_code . '<div id="tpNewsTab"> <ul class="resp-tabs-list vert"> ' . $tabs . ' </ul> <div class="resp-tabs-container vert"> ' . $tab_content . ' </div> </div>'; } } else { echo "請安裝 CURL"; }
PHP腳本
上中
正式釋出