.aspx),大量 __VIEWSTATE、__EVENTVALIDATION 隱藏字段。class CurlSession {
private $ch;
private $cookieFile;
private $userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36';
public function __construct(string $cookieFile = '') {
$this->cookieFile = $cookieFile ?: sys_get_temp_dir() . '/cookie_' . md5(microtime()) . '.txt';
$this->ch = curl_init();
curl_setopt_array($this->ch, [
CURLOPT_COOKIEJAR => $this->cookieFile,
CURLOPT_COOKIEFILE => $this->cookieFile,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_AUTOREFERER => true,
CURLOPT_USERAGENT => $this->userAgent,
CURLOPT_SSL_VERIFYPEER => false, // 測試環境,生產需 true + cacert
CURLOPT_TIMEOUT => 30,
]);
}
public function get(string $url, array $headers = []): string {
curl_setopt($this->ch, CURLOPT_HTTPGET, true);
curl_setopt($this->ch, CURLOPT_URL, $url);
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
return $this->exec();
}
public function post(string $url, array $data, array $headers = []): string {
curl_setopt($this->ch, CURLOPT_POST, true);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($this->ch, CURLOPT_URL, $url);
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
return $this->exec();
}
private function exec(): string {
$resp = curl_exec($this->ch);
if (curl_errno($this->ch)) throw new RuntimeException(curl_error($this->ch));
return $resp;
}
public function __destruct() { curl_close($this->ch); }
}
$client = new CurlSession();
$loginPage = $client->get('https://jw.example.edu.cn/default2.aspx');
$viewState = '';
if (preg_match('/id="__VIEWSTATE" value="([^"]+)"/', $loginPage, $m)) $viewState = $m[1];
$eventValidation = '';
if (preg_match('/id="__EVENTVALIDATION" value="([^"]+)"/', $loginPage, $m)) $eventValidation = $m[1];
$postData = [
'__VIEWSTATE' => $viewState,
'__EVENTVALIDATION' => $eventValidation,
'txtUserName' => $studentId,
'TextBox2' => $password,
'txtSecretCode' => '', // 關鍵:留空繞過
'RadioButtonList1' => iconv('UTF-8', 'GB2312', '學生'), // 編碼轉換
'Button1' => iconv('UTF-8', 'GB2312', '登錄'),
'lbLanguage' => '',
'hidPdrs' => '',
'hidsc' => '',
];
$loginResult = $client->post('https://jw.example.edu.cn/default2.aspx', $postData);
// 判斷登入成功:檢查是否重定向到主頁、或頁面包含用戶名
if (strpos($loginResult, '學生') === false) {
throw new Exception('登入失敗:' . strip_tags($loginResult));
}
get($scheduleUrl);
$viewState2 = '';
if (preg_match('/id="__VIEWSTATE" value="([^"]+)"/', $schedulePage, $m)) $viewState2 = $m[1];
$viewState2,
'__EVENTTARGET' => '',
'__EVENTARGUMENT' => '',
'xnd' => '2023-2024', // 學年
'xqd' => '1', // 學期 1/2
'Button1' => iconv('UTF-8', 'GB2312', '確定'),
];
$scheduleHtml = $client->post($scheduleUrl, $postData2);
function parseScheduleTable(string $html): array {
libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHTML('' . $html); // 強制聲明編碼
libxml_clear_errors();
$xpath = new DOMXPath($dom);
$rows = $xpath->query('//table[@id="Table1"]//tr'); // 根據實際 ID 調整
$schedule = [];
foreach ($rows as $i => $row) {
if ($i === 0) continue; // 跳過表頭
$cells = $xpath->query('td', $row);
$dayData = [];
foreach ($cells as $j => $cell) {
if ($j === 0) continue; // 第一列為節次
$text = trim($cell->textContent);
// 解析 "課程名\n老師\n教室" 多行文本
$lines = array_filter(array_map('trim', explode("\n", $text)));
$dayData[] = [
'course' => $lines[0] ?? '',
'teacher' => $lines[1] ?? '',
'room' => $lines[2] ?? '',
];
}
if (!empty(array_filter($dayData))) $schedule[] = $dayData;
}
return $schedule;
}
$schedule = parseScheduleTable($scheduleHtml);
// $schedule[週幾][節次] = ['course'=>..., 'teacher'=>..., 'room'=>...]
class ZhengfangSchedule {
private CurlSession $client;
private string $baseUrl;
public function __construct(string $baseUrl) { $this->baseUrl = rtrim($baseUrl, '/'); $this->client = new CurlSession(); }
public function login(string $username, string $password): bool { /* 步驟1-2 */ return true; }
public function getSchedule(string $studentId, string $year, string $term): array { /* 步驟3-5 */ return []; }
}
模擬登錄 ASP.NET 系統核心在於:**會話 Cookie 持久化、__VIEWSTATE/__EVENTVALIDATION 動態提取、編碼轉換 (UTF-8 ↔ GB2312)、驗證碼邏輯繞過**。實際開發中,**優先申請官方 API 或數據同步接口**,避免脆弱的頁面解析方案。