雲端校務系統API應用-同步學期資料實作分享

1.目前同步學期資料API應用可回傳資料有基本資訊,如更新時間、學年學期學期開始日期學期結束日期開學日結業日,及有用學期資料如學期編班學期教職員單位資料課表。

 2.因為要在本校的線上請假系統強化調代課功能,故本範例主要實作透過雲端校務系統API應用,取回課表資料留存本地端,並透過比對校務系統和本地端的更新時間資訊,達成自動化更新功能。

3.程式碼如下:

 <?php
include_once("config.php") ;
session_start();
function objectToArray($d) {
        if (is_object($d)) {
            $d = get_object_vars($d);
        }
        
        if (is_array($d)) {
            return array_map(__FUNCTION__, $d);
        }
        else {
            return $d;
        }
    }
echo "<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'></head>" ;
if (in_array($_SESSION['login_title'], $admin_title))
  {  
       // API NAME
     $api_name = '/semester-data';
     // API URL
     $api_url = 'https://api.tc.edu.tw';
     // 建立 CURL 連線
     $ch = curl_init();
     // 取 access token
     curl_setopt($ch, CURLOPT_URL, $api_url."/oauth?authorize");
     // 設定擷取的URL網址
     curl_setopt($ch, CURLOPT_POST, TRUE);
     // the variable
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
     //$clientId 和 $client_secret 的值於 config.php 中取得,設定需在各校雲端系統中以系統管理者權限為之。
     curl_setopt($ch, CURLOPT_POSTFIELDS, array(
     'client_id' => $clientId,
     'client_secret' => $client_secret,
     'grant_type' => 'client_credentials'
     ));
       $data = curl_exec($ch);
     $data = json_decode($data);
     $access_token = $data->access_token;
     $authorization = "Authorization: Bearer ".$access_token;
     curl_setopt($ch, CURLOPT_URL, $api_url.$api_name);
     curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization )); // **Inject Token into Header**
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
     $result = curl_exec($ch);
     $data = json_decode($result);
     $data = objectToArray($data) ;
     //解決CSV首字亂碼
     setlocale(LC_ALL, 'en_US.UTF-8');
     $fname = "files/semester_data/class.csv" ;
     if (file_exists($fname))
       {
             //若本地端已有課表資料csv檔,比對雲端校務系統 semester_data 更新時間 和既存檔案更新時間,若時間不同,刪檔重建較新資料。
            if (($handle = fopen($fname, "r")) !== FALSE)
            {
               while (($row_data = fgetcsv($handle)) !== FALSE)
                 {
                     if ($row_data[0] == '更新時間')
                       {
                           $local_update_time =     $row_data[1] ;
                       }
                 }
               fclose($handle) ;
            }
          if ($local_update_time != $data['更新時間'])
            {
                   unlink($fname) ;
                   echo "<script>location.href= ('sync_semester_data.php');</script>" ;
            }     
       } else {
                    //課表資料csv檔不存在,連線雲端校務系統產生此檔。
                 $fp = fopen($fname, 'w');
                 foreach ($data as $key => $value)
                   {
                        if (!is_array($value))
                         {
                              //記錄更新時間 學年 學期 學期開始日期 學期結束日期 開學日 結業日
                              fwrite($fp, $key.",".$value."\n");
                         }
                      if (is_array($value) && ($key == '課表'))
                         {
                             //本例只需要課表資料,每堂課csv資料格式如下
                               //年級,班序,班名,星期,節次,科目,專科教室,教師,身分證編碼
                               foreach ($value as $value2)
                                 {
                                     $num = 0 ;
                                      foreach ($value2 as $value3)
                                          {
                                                $num++ ;
                                                if ($num == count($value2))
                                                  {
                                                        fwrite($fp, $value3."\n");
                                                  } else {
                                                           fwrite($fp, $value3.",");
                                                         }
                                          }
                                 }                  
                         }
                   }
                 fclose($fp);
             }
     //顯示
     echo "<body bgcolor='#e7d6b6'>" ;
     echo "<style>a { text-decoration:none;}</style>" ;
     echo "<center><br><img src='images/sync.png' width='80'><br><br>" ;
     echo "<b><font size='5'>自動同步雲端校務系統課表資料後取得最新版本時間&nbsp;:</font>" ;
     echo "<br><br><font size='6' color='blue'>".$data['更新時間']."</font></b>" ;
     echo "</center>" ;
  } else {
           echo "<script>alert ('您尚未登入或無權限瀏覽本網頁!!'); location.href= ('index.php');</script>" ;           
         }
?>

瀏覽數: