diff --git a/app/controller/Crawler.php b/app/controller/Crawler.php index 097f018..c3ab17f 100644 --- a/app/controller/Crawler.php +++ b/app/controller/Crawler.php @@ -29,6 +29,7 @@ class Crawler extends BaseController $examid = $this->request->param('examid', ''); $bmid = $this->request->param('bmid', ''); $userid = $this->request->param('userid', ''); + $cookiesParam = $this->request->param('cookies', ''); if (empty($examid) || empty($bmid) || empty($userid)) { return json([ @@ -36,6 +37,24 @@ class Crawler extends BaseController 'msg' => '请先填写examid、bmid和userid', ]); } + + if (empty($cookiesParam)) { + return json([ + 'code' => 0, + 'msg' => '请填写Cookie数据', + ]); + } + + // 解析JSON格式的cookies + $cookies = is_string($cookiesParam) ? json_decode($cookiesParam, true) : $cookiesParam; + if (json_last_error() !== JSON_ERROR_NONE || empty($cookies)) { + return json([ + 'code' => 0, + 'msg' => 'Cookie数据格式错误,请检查JSON格式', + ]); + } + + $cookieString = $this->buildCookieString($cookies); // 构建URL获取HTML $url = "http://gzrsks.oumakspt.com:62/tyzpwb/stuchooseexam/selectPosition.htm?examstupid=1015&userid={$userid}&bmid={$bmid}&examid={$examid}&aa=" . time() . '000'; @@ -45,6 +64,11 @@ class Crawler extends BaseController curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + 'Cookie: ' . $cookieString, + 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36', + 'Referer: http://gzrsks.oumakspt.com:62/tyzpwb/', + ]); $html = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); @@ -82,6 +106,24 @@ class Crawler extends BaseController } } + /** + * 构建Cookie字符串 + * @param array $cookies Cookie数组 + * @return string + */ + private function buildCookieString(array $cookies): string + { + $cookieArray = []; + + $cookieData = $cookies['请求 Cookie'] ?? $cookies; + + foreach ($cookieData as $key => $value) { + $cookieArray[] = $key . '=' . $value; + } + + return implode('; ', $cookieArray); + } + /** * 获取职位代码列表 */ diff --git a/view/crawler/index.html b/view/crawler/index.html index 3fda856..a792230 100644 --- a/view/crawler/index.html +++ b/view/crawler/index.html @@ -243,10 +243,22 @@