青书学堂 看视频 看课时 php 懒人版
上一篇关于青书学堂的 操作起来 有点麻烦
趁这几天有时间 优化了一下
建议php 7.3 版本
(本程序会用到php里的curl 模块 记得打开)
如果运行时 获取信息空白(https 容易出现) 可以测试一下自己php的curl能不能正常用 如果不能可以参考一下我的另一篇文章
SSL rtificate problem: unable to get local issuer certificate curl采集https数据采集不到-CSDN博客
只需要登录网页版青书学堂 获取到请求的 cookie
把上面的cookie 改到文件里 的 第5行
<?php
// 需要改成自己的
// 自己的 cookie 需要更改一下 具体可以查看 浏览器的请求头cookie ______________________________
// define('USER_COOKIE', '改成自己的');// 当前学校索引 _____________
define('SCHOOL_INDEX', 0); // 默认第一个学校// 查看的分钟数 建议 ________________
define('MINUTE', 60); // 分钟// 当前学校 前缀
define('SCHOOL_PREFIX', getSchool());// 获取学校
function getSchool() {$curl = curl_init();curl_setopt_array($curl, array(CURLOPT_URL => 'https://www.qingshuxuetang.com/GetUserOrganizations',CURLOPT_RETURNTRANSFER => true,CURLOPT_CUSTOMREQUEST => 'GET',CURLOPT_HTTPHEADER => array('Cookie: ' . USER_COOKIE,),));$response = curl_exec($curl);curl_close($curl);$response = json_decode($response, true);$symbol = '';if (isset($response['data']) && count($response['data']) > 0) {$degrees = $response['data']['degrees']??[];foreach ($degrees as $dk => $dv) {if (SCHOOL_INDEX == $dk) {echo '看视频的';}echo '学校 ' . $dv['name'] . ' 索引 ' . $dk . PHP_EOL;}$symbol = $degrees[SCHOOL_INDEX]['symbol'];}if (empty($symbol)){dd('获取学校信息失败');}return $symbol;
}// 获取所有课程
function getCourse()
{$curl = curl_init();curl_setopt_array($curl, array(CURLOPT_URL => 'https://degree.qingshuxuetang.com/' . SCHOOL_PREFIX . '/Student/Course/CourseData?_t=' . time() * 1000,CURLOPT_CUSTOMREQUEST => 'POST',CURLOPT_RETURNTRANSFER => true,CURLOPT_HTTPHEADER => array('Accept:application/json','Cookie: ' . USER_COOKIE,'Content-Type:application/json','Device-Trace-Id-QS: 7a89ee95-1573-4306-a554-73ccb49e2dfc','User-Agent: Mozilla/5.0 windows12 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0',),));$response = curl_exec($curl);curl_close($curl);$response = json_decode($response, true);if (isset($response['message']) && $response['message'] == '成功') {return $response['data']??[];}dd('获取课程信息失败');
}$course = getCourse();
$currentSource = [];
foreach ($course as $key => $value) {if ($value['isCurrent'] == true) {$currentSource[] = $value;}
}
if (empty($currentSource)) {dd('没有获取到要学习的课程');
}
$courseIds = [];
foreach ($currentSource as $cuk => $cuv) {$courseId = $cuv['courseId'];$teachPlanId = $cuv['teachPlanId'];$periodId = $cuv['periodId'];$courseName = $cuv['courseName'];echo $courseName . ' 获取章节信息中' . PHP_EOL;$teachingPlanCourseId = $cuv['teachingPlanCourseId'];$chapterUrl = getChapterUrl($courseId, $teachPlanId, $periodId);$chapter = getChapter($chapterUrl);$courseIds[] = ['teachPlanId' => $teachPlanId,'courseId' => $courseId,'courseIds' => getChapterIds($chapter),'courseName' => $courseName,'periodId' => $periodId,'teachingPlanCourseId' => $teachingPlanCourseId,];
}$refreshData = [];
foreach ($courseIds as $cik => $civ) {$chapterIds = $civ['courseIds'];foreach ($chapterIds as $ck => $cv) {echo $civ['courseName'] . ' 第[ ' . ($ck+1) . ' ]节 初始化学习中 ' . PHP_EOL;list($beginId, $referer) = begin($civ, $cv);$refreshData[] = ['courseId' => $civ['courseId'],'contentId' => $cv,'beginId' => $beginId,'referer' => $referer,];}
}$i = (MINUTE * 6);
while ($i > 0) {$i--;echo '还剩 ' . $i . ' 次刷新' . PHP_EOL;foreach ($refreshData as $rdk => $rdv) {refresh($rdv['beginId'], $rdv['courseId'], $rdv['contentId'], $rdv['referer']);}sleep(10);
}
echo '结束';// 获取章节链接
function getChapterUrl($courseId, $teachPlanId, $periodId)
{$curl = curl_init();curl_setopt_array($curl, array(CURLOPT_URL => "https://degree.qingshuxuetang.com/" . SCHOOL_PREFIX . "/Student/Course/CourseStudy?courseId=$courseId&teachPlanId=$teachPlanId&periodId=$periodId",CURLOPT_RETURNTRANSFER => true,CURLOPT_CUSTOMREQUEST => 'GET',CURLOPT_HTTPHEADER => array('Cookie: ' . USER_COOKIE,'User-Agent: Mozilla/5.0 windows12 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0',),));$response = curl_exec($curl);if (empty($response)) {dd('获取章节链接 获取失败');}$reg = '/Svc\/GetCoursewareTree\?(.*?)\"/is';preg_match($reg, $response, $matches);curl_close($curl);if (isset($matches[1]) && !empty($matches[1])) {return $matches[1];}dd('获取章节链接失败');
}// 获取章节信息
function getChapter($chapterUrl)
{$curl = curl_init();curl_setopt_array($curl, array(CURLOPT_URL => 'https://degree.qingshuxuetang.com/' . SCHOOL_PREFIX . '/Svc/GetCoursewareTree?' . $chapterUrl,CURLOPT_RETURNTRANSFER => true,CURLOPT_CUSTOMREQUEST => 'GET',CURLOPT_HTTPHEADER => array('Cookie: ' . USER_COOKIE,'User-Agent: Mozilla/5.0 windows12 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0',),));$response = curl_exec($curl);if (empty($response)) {dd('获取章节信息 获取失败');}$response = json_decode($response, true);$data = $response['data']??[];if (empty($data)) {dd('获取章节信息 为空');}return $data;
}// 获取课程id
function getChapterIds($data) {$courseData = [];if (isset($data['nodes']) && count($data['nodes']) > 0) {$tmpNodes = $data['nodes'];foreach ($tmpNodes as $tnk => $tnv) {// 判断是否为视频 ['html', 'video']if (in_array($tnv['type'], ['video'])) {$courseData[] = $tnv['id'];} else {$courseData = array_merge($courseData, getChapterIds($tnv));}}}return $courseData;
}// 开始学习
function begin($civ, $contentId)
{$courseId = $civ['courseId'];$curl = curl_init();$referer = "https://degree.qingshuxuetang.com/" . SCHOOL_PREFIX . "/Student/Course/CourseShow?teachPlanId=" . $civ['teachPlanId'] . "&periodId=" . $civ['periodId'] . "&courseId=$courseId&nodeId=$contentId";curl_setopt_array($curl, array(CURLOPT_URL => 'https://degree.qingshuxuetang.com/' . SCHOOL_PREFIX . '/Student/Course/UploadStudyRecordBegin?_t=' . (time() * 1000),CURLOPT_RETURNTRANSFER => true,CURLOPT_ENCODING => '',CURLOPT_MAXREDIRS => 10,CURLOPT_TIMEOUT => 0,CURLOPT_FOLLOWLOCATION => true,CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,CURLOPT_CUSTOMREQUEST => 'POST',CURLOPT_POSTFIELDS => '{"classId": "' . $civ['teachPlanId'] . '","courseId": "' . $courseId . '","contentId": "' . $contentId . '","contentType": 11,"periodId": "' . $civ['periodId'] . '","position": 0,"detectId": null}',CURLOPT_HTTPHEADER => array('Cookie: ' . USER_COOKIE,'User-Agent: Mozilla/5.0 windows12 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0','Content-Type:application/json',"Referer: $referer",),));$response = curl_exec($curl);// dd($response);echo 'begin 请求结果:' . $response . PHP_EOL;curl_close($curl);$response = json_decode($response, true);if (isset($response['message']) && $response['message'] == '成功') {return [$response['data'], $referer];} else {echo '开始学习 请求失败' . PHP_EOL;dd($response);}
}// 刷新当前学习进度
function refresh($id, $courseId, $contentId, $referer)
{$curl = curl_init();curl_setopt_array($curl, array(CURLOPT_URL => 'https://degree.qingshuxuetang.com/' . SCHOOL_PREFIX . '/Student/Course/UploadStudyRecordContinue?_t=' . (time() * 1000),CURLOPT_RETURNTRANSFER => true,CURLOPT_ENCODING => '',CURLOPT_MAXREDIRS => 10,CURLOPT_TIMEOUT => 0,CURLOPT_FOLLOWLOCATION => true,CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,CURLOPT_CUSTOMREQUEST => 'POST',CURLOPT_POSTFIELDS => array('recordId' => $id, 'end' => 'true', 'position' => '3000', 'timeOutConfirm' => 'false'),CURLOPT_HTTPHEADER => array('Cookie: ' . USER_COOKIE,'Origin: https://degree.qingshuxuetang.com',"Referer: $referer",'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 Edg/122.0.0.0',),));$response = curl_exec($curl);// dd(curl_errno($curl));echo 'refresh 请求结果:' . $response . PHP_EOL;curl_close($curl);$response = json_decode($response, true);if (isset($response['message']) && $response['message'] == '成功') {return true;} else {echo '响应错误';// dd($response);}
}// 打印输出
function dd($data, $var_dump = false)
{echo PHP_EOL;if ($var_dump) {var_dump($data);} else {print_r($data);}die();
}
保存一下文件 比如 保存为 index.php 然后 就可以了
php index.php
回车后 出现这样的提示 即表示成功 目前默认时间为1个小时 一个小时后 当前学期的课程就都是 看完一个小时后的了
下图为 运行了40多分钟左右的样子
如果对你有帮助 麻烦给个一键三连 有不会的 欢迎随时打扰~