$url_arr[xxx]
最右邊的 $limit
,改成數字,即可自行設定該分類顯示數量。此區塊可以擷取台北市教育局公告的json檔,並即時呈現公告。
其中 $limit 可以自訂呈現篇數
若有不需要的項目,請自行註記 $url_arr 即可(在最左邊加上//)。
$limit = 5; //不需要的公告,請自行註解掉(在最左邊加上//,右邊的 $limit 可以直接改成數值,自己設定顯示篇數) $url_arr['一般公告'] = array('https://www.doe.gov.taipei/OpenData.aspx?SN=A53E0BA10DD4457E', $limit); $url_arr['幼兒園'] = array('https://www.doe.gov.taipei/OpenData.aspx?SN=D5AD2C5A7A85502C', $limit); $url_arr['國小'] = array('https://www.doe.gov.taipei/OpenData.aspx?SN=A06275FDC88942AB', $limit); //$url_arr['國中'] = array('https://www.doe.gov.taipei/OpenData.aspx?SN=2376933CA35541CB', $limit); //$url_arr['高中職'] = array('https://www.doe.gov.taipei/OpenData.aspx?SN=8FDEB38F0F17B561', $limit); $url_arr['終身教育'] = array('https://www.doe.gov.taipei/OpenData.aspx?SN=711FA93AC6D09A71', $limit); 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_data) { list($url, $limit) = $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)); } $tabs .= "<li>$tab_title</li>"; if ($data) { if (substr($data, 0, 3) == pack("CCC", 0xef, 0xbb, 0xbf)) { $data = substr($data, 3); } $news_arr = json_decode($data, true); $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>'; }
PHP腳本
上中
$url_arr[xxx]
最右邊的 $limit
,改成數字,即可自行設定該分類顯示數量。正式釋出