參考「課程格子」「超級課程表」思路,利用 PHP cURL 模擬學生登錄教務系統,自動抓取課表、空教室數據,構建個性化查詢服務。
正方登錄頁驗證碼圖片由單獨請求生成。若瀏覽器不請求該圖片 URL,後端就不會生成 Session 中的驗證碼值。直接提交空驗證碼即可通過驗證。可在代碼層面直接忽略驗證碼請求。
function curl_request($url, $post = '', $cookie = '', $returnCookie = 0) {
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_AUTOREFERER => 1,
CURLOPT_REFERER => '教務系統登錄頁URL', // 必須設置
CURLOPT_TIMEOUT => 20,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => false, // 教務系統多為 HTTP 或自簽證書
]);
if ($post) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
}
if ($cookie) curl_setopt($ch, CURLOPT_COOKIE, $cookie);
if ($returnCookie) curl_setopt($ch, CURLOPT_HEADER, 1);
$data = curl_exec($ch);
if (curl_errno($ch)) return curl_error($ch);
curl_close($ch);
if ($returnCookie) {
list($header, $body) = explode("\r\n\r\n", $data, 2);
preg_match_all("/Set-Cookie:([^;]*);/", $header, $matches);
return ['cookie' => substr($matches[1][0], 1), 'content' => $body];
}
return $data;
}
__VIEWSTATE 動態獲取function getViewState($url, $cookie = '') {
$html = curl_request($url, '', $cookie);
preg_match('//i', $html, $m);
return $m[1] ?? '';
}
function login($username, $password) {
$url = '教務系統登錄地址';
$post = [
'__VIEWSTATE' => getViewState($url),
'txtUserName' => $username,
'TextBox2' => $password,
'txtSecretCode' => '', // 利用漏洞留空
'RadioButtonList1' => iconv('utf-8', 'gb2312', '學生'),
'Button1' => iconv('utf-8', 'gb2312', '登錄'),
];
$result = curl_request($url, $post, '', 1);
return $result['cookie']; // 返回帶有 Session 的 Cookie
}
xskbcx.aspx#Table1 表格內容<td> 單元格,提取課程信息(含 {周一 1,2} 格式定位)[周一 => [1,2 => '課程名', ...], ...] 結構化數組xxjsjy.aspx 獲取新 __VIEWSTATEiconv('utf-8','gb2312',...),響應解析需反向轉換不同學校教務系統頁面結構、字段名稱均有差異。核心思路不變:抓包分析 → 補全字段 → 編碼轉換 → 正則解析 → 結構化輸出。遇到問題可郵件交流:imzhongshan@126.com