PHP代码说明
优学院自动刷课PHP代码,功能不太完善,但核心代码都有,如果你会一点PHP也能自己完善,当然你是纯小白,我也打包好了可执行文件,直接运行即可。
刚开始用PHP是想写一个接口,后来在获取视频时长的时候卡住了,太耗时了,经常性500,很不实用(如果你用命令行执行PHP当我没说)
PHP代码功能
1. 自动刷课
2. 自动答题
3. 自定义多个章节
4. 自定义章节学习时间
那个倍速脚本有上限(好像是16倍),用这个直接秒
这个是自动获取视频时长的,不用担心这个个问题
PHP代码示例
<?php
header(
'Content-Type:text/json;charset=utf-8'
);
//用户名
$username
=
""
;
//密码
$password
=
""
;
$public_param
= [];
echo
"GitHub主页:https://github.com/Chirmis(start走一走)"
;
$cookie
=
file_get_contents
(
'./cookies.txt'
);
if
(
$cookie
==
''
) {
// 命令行使用
// echo "请输入用户名:\n";
// $username = fgets(STDIN);
// echo $username;
// echo "请输入密码:\n";
// $password = fgets(STDIN);
// echo $password;
echo
"开始登陆.....\n"
;
login(
$username
,
$password
);
echo
"登陆成功.....\n"
;
$cookie
=
file_get_contents
(
'./cookies.txt'
);
}
echo
"获取课程列表.....\n"
;
$courses_list_json
= get_courses_list(
$cookie
);
//课程数据
$courses_list_arr
= json_decode(
$courses_list_json
, true);
echo
"查询到"
.
$courses_list_arr
[
'total'
].
"门课程\n"
;
for
(
$i
=0;
$i
<
count
(
$courses_list_arr
[
'courseList'
]);
$i
++){
echo
"课程编号:"
.
$i
.
"; 课程名称:"
.
$courses_list_arr
[
'courseList'
][
$i
][
'name'
].
"\n"
;
}
echo
"请选择课程编号:\n"
;
//该值可更改(在课程编号之间)
$coursesId
= 4;
//从键盘输入
// $coursesId = (int)fgets(STDIN);
echo
"当前选择课程编号为:$coursesId; 课程名为:"
.
$courses_list_arr
[
'courseList'
][
$coursesId
][
'name'
] .
"\n"
;
echo
"开始获取课程章节...\n"
;
$course_chapters_list_json
= get_course_chapters_list(
$courses_list_arr
[
'courseList'
][
$coursesId
][
'id'
]);
//包含该课程下有几门小课程(textbook),以及班级信息(classInfo)
$course_chapters_list_arr
= json_decode(
$course_chapters_list_json
, true);
$textbookNum
=
count
(
$course_chapters_list_arr
[
'textbook'
]);
echo
"该课程下有"
.
$textbookNum
.
"门课程\n"
;
for
(
$i
=0;
$i
<
$textbookNum
;
$i
++) {
echo
"第"
.
$i
.
"门课程; 课程名为:"
.
$course_chapters_list_arr
[
'textbook'
][
$i
][
'textbook'
][
'courseName'
].
"\n"
;
}
echo
"请选择小课程编号:\n"
;
//当一门课程下有多门小课程的时候,此变量可改,否则只能为0
if
(
$textbookNum
== 1){
$chooseTextbookId
= 0;
}
else
{
$chooseTextbookId
= 0;
//从键盘输入
// $chooseTextbookId = (int)fgets(STDIN);
}
echo
"当前选择小课程编号为:"
.
$chooseTextbookId
.
"课程名为:"
.
$course_chapters_list_arr
[
'textbook'
][
$chooseTextbookId
][
'textbook'
][
'courseName'
].
"\n"
;
$textbookInfo
=
$course_chapters_list_arr
[
'textbook'
][
$chooseTextbookId
][
'textbook'
];
$textbookId
=
$course_chapters_list_arr
[
'textbook'
][
$chooseTextbookId
][
'textbook'
][
'courseId'
];
$classId
=
$course_chapters_list_arr
[
'classInfo'
][
'classId'
];
//(textbookId, classId)
echo
"正在获取"
.
$course_chapters_list_arr
[
'textbook'
][
$chooseTextbookId
][
'textbook'
][
'courseName'
].
"课程下的章节内容\n"
;
$chapters_item_json
= get_chapters_item(
$textbookId
,
$classId
);
$chapters_item_arr
= json_decode(
$chapters_item_json
, true);
$chapters_item_num
=
count
(
$chapters_item_arr
[
'chapters'
]);
echo
$chapters_item_arr
[
'coursename'
] .
"下共有:"
.
$chapters_item_num
.
"章节\n"
;
for
(
$j
=3;
$j
<
$chapters_item_num
;
$j
++) {
echo
"章节编号:"
.
$j
.
"; 章节名称:"
.
$chapters_item_arr
[
'chapters'
][
$j
][
'nodetitle'
].
"\n"
;
echo
"请选择章节编号:\n"
;
$chapterNumber
=
$j
;
echo
"当前所选章节编号为:"
.
$chapterNumber
.
"章节名称为:"
.
$chapters_item_arr
[
'chapters'
][
$chapterNumber
][
'nodetitle'
].
"\n"
;
echo
"正在获取"
.
$chapters_item_arr
[
'chapters'
][
$chapterNumber
][
'nodetitle'
].
"下所有小节内容\n"
;
$nodeid
=
$chapters_item_arr
[
'chapters'
][
$chapterNumber
][
'nodeid'
];
//chapters[$i]['nodeid']
echo
get_every_section(
$nodeid
);
}
// echo "请选择章节编号:\n";
// $chapterNumber= 1;
// echo "当前所选章节编号为:" . $chapterNumber . "章节名称为:" . $chapters_item_arr['chapters'][$chapterNumber]['nodetitle']."\n";
// echo "正在获取" .$chapters_item_arr['chapters'][$chapterNumber]['nodetitle']."下所有小节内容\n";
// $nodeid = $chapters_item_arr['chapters'][$chapterNumber]['nodeid'];
// //chapters[$i]['nodeid']
// echo get_every_section($nodeid);
/**
* 获取测验的答案
* questionidid在 $wholepageItemDTOList[$i]['wholepageDTOList'][0]['coursepageDTOList'][$j]['questionDTOList'][0]['questionid']
* https://api.ulearning.cn/questionAnswer/$questionidid
*/
//获取当前章节下小节内容
function
get_now_chapter_section(
$itemid
)
{
global
$public_param
;
$api
=
"https://api.ulearning.cn/wholepage/chapter/stu/$itemid"
;
$params
=
array_merge
(
$public_param
,
array
());
$json
= curl(
$api
,
'GET'
,
$params
);
$data
= json_decode(
$json
, true);
$wholepageItemDTOList
=
$data
[
'wholepageItemDTOList'
];
for
(
$i
=0;
$i
<
count
(
$wholepageItemDTOList
);
$i
++){
//图文型数据
if
(
$wholepageItemDTOList
[
$i
][
'wholepageDTOList'
][0][
'contentType'
] == 5){
echo
"图文型数据"
;
$postData
= [
'pageid'
=> 462250,
'complete'
=> 1,
'studyTime'
=> 1000,
'score'
=> 100,
'answerTime'
=> 1,
'submitTimes'
=> 0,
'questions'
=> [],
'videos'
=> [],
'speaks'
=> [],
];
}
//视频型数据
if
(
$wholepageItemDTOList
[
$i
][
'wholepageDTOList'
][0][
'contentType'
] == 6){
echo
"视频型数据"
;
}
//测验型数据
if
(
$wholepageItemDTOList
[
$i
][
'wholepageDTOList'
][0][
'contentType'
] == 7){
echo
"测验型数据"
;
}
}
}
//获取当前所选章节内容详情
function
get_now_chapters_info(
$courseId
,
$classId
)
{
global
$public_param
;
$now_chapters_info_api
=
"https://api.ulearning.cn/course/stu/$courseId/directory?classId=$classId"
;
$params
=
array_merge
(
$public_param
,
array
());
return
curl(
$now_chapters_info_api
,
'GET'
,
$params
);
}
//登陆获取cookie
function
login(
$username
,
$password
)
{
$login_api
=
"https://www.ulearning.cn/umooc/user/login.do"
;
$data
= [
'name'
=>
$username
,
'passwd'
=>
$password
,
'yancode'
=>
''
,
'redirectURL'
=>
''
,
'isFrom'
=>
''
,
'newLocale'
=>
''
,
];
$post_data
= http_build_query(
$data
);
$params
= [
'data'
=>
$post_data
,
'header'
=> [
'User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.100 Safari/537.36'
,
],
'ResponseHeaders'
=> true,
'NOBODY'
=> true
];
$res
= curl(
$login_api
,
'POST'
,
$params
);
preg_match_all(
"/Set-Cookie: (.*?)\; Domain/"
,
$res
,
$data
);
for
(
$i
=0;
$i
<
count
(
$data
[1]);
$i
++){
$data
[1][
$i
] = urldecode(
$data
[1][
$i
]);
$cookie
.=
$data
[1][
$i
].
";"
;
}
file_put_contents
(
'./cookies.txt'
,
$cookie
);
}
//个人中心首页获取课程列表
function
get_courses_list(
$cookie
)
{
global
$public_param
;
$data
=
explode
(
";"
,
$cookie
);
for
(
$i
=0;
$i
<
count
(
$data
);
$i
++){
$k_v
=
explode
(
"="
,
$data
[
$i
]);
$k
=
$k_v
[0];
$v
=
$k_v
[1];
$res_arr
[
$i
][
$k
] =
$v
;
if
(json_decode(
$res_arr
[
$i
][
$k
]) != null) {
$res_arr
[
$i
][
$k
] = json_decode(
$res_arr
[
$i
][
$k
], true);
}
}
$Authorization
=
$res_arr
[1][
'AUTHORIZATION'
];
$token
=
$res_arr
[5][
'token'
];
$public_param
= [
'header'
=> [
"Authorization: $Authorization"
,
"UA-AUTHORIZATION: $token"
,
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.100 Safari/537.36"
,
],
];
//学生首页课程API
$api
=
"https://courseapi.ulearning.cn/courses/students?keyword=&publishStatus=1&type=1&pn=1&ps=15"
;
$params
=
array_merge
(
$public_param
,
array
());
return
curl(
$api
,
'GET'
,
$params
);
}
//获取课程下章节列表
function
get_course_chapters_list(
$courseListId
)
{
global
$public_param
;
$params
=
array_merge
(
$public_param
,
array
());
//获取课程的子课程(一门课有多本书)
$get_textbookId_api
=
"https://courseapi.ulearning.cn/textbook/student/$courseListId/list"
;
$listJson
= curl(
$get_textbookId_api
,
'GET'
,
$params
);
//获取该课程的班级号
$get_course_classId_api
=
"https://courseapi.ulearning.cn/classes/information/student/$courseListId"
;
$classIdJson
= curl(
$get_course_classId_api
,
'GET'
,
$params
);
$classData
= json_decode(
$classIdJson
, true);
//课程下多本书
$listData
= json_decode(
$listJson
, true);
for
(
$i
=0;
$i
<
count
(
$listData
);
$i
++){
// echo "当前课程为:".$listData[$i]['name']."\n";
$textbookId
=
$listData
[
$i
][
'courseId'
];
$get_course_chapters_list_api
=
"https://courseapi.ulearning.cn/textbook/student/information?ocId=$courseListId&textbookId=$textbookId"
;
$data
[
'textbook'
][
$i
] = json_decode(curl(
$get_course_chapters_list_api
,
'GET'
,
$params
), true);
}
$data
[
'classInfo'
] =
$classData
;
return
json_encode(
$data
);
}
//进入课程播放器,获取章节下所有小节
function
get_chapters_item(
$textbookId
,
$classId
)
{
global
$public_param
;
$params
=
$public_param
;
$get_directory_info_api
=
"https://api.ulearning.cn/course/stu/$textbookId/directory?classId=$classId"
;
$directoryInfo
= curl(
$get_directory_info_api
,
'GET'
,
$params
);
//chapters下所有items就是本章所有小节的内容(每个item中的nodeid都是获取小节下的内容标识)
return
$directoryInfo
;
}
//获取每小节下的小节(需要循环)
function
get_every_section(
$nodeid
)
{
global
$public_param
;
$get_every_section_api
=
"https://api.ulearning.cn/wholepage/chapter/stu/$nodeid"
;
$sectionJson
= curl(
$get_every_section_api
,
'GET'
,
$public_param
);
$sectionData
= json_decode(
$sectionJson
, true);
$get_user_info_api
=
"https://api.ulearning.cn/user"
;
$user_info_json
= curl(
$get_user_info_api
,
'GET'
,
$public_param
);
$userInfo
= json_decode(
$user_info_json
, true);
for
(
$i
=0;
$i
<
count
(
$sectionData
[
'wholepageItemDTOList'
]);
$i
++){
check_section(
$sectionData
[
'wholepageItemDTOList'
][
$i
],
$userInfo
);
}
echo
"本小节课程已刷完\n"
;
}
//发送请求
function
check_section(
$wholepageItemDTOList
,
$userInfo
)
{
global
$public_param
;
$cryptoJsonData
= [
'itemid'
=>
$wholepageItemDTOList
[
'itemid'
],
'autoSave'
=> 1,
'version'
=>
''
,
'withoutOld'
=>
''
,
'complete'
=> 1,
'userName'
=>
$userInfo
[
'name'
],
'score'
=> 100,
'pageStudyRecordDTOList'
=> [],
];
$temple
= [
'pageid'
=> 0,
'complete'
=> 1,
'studyTime'
=> 400,
'score'
=> 100,
'answerTime'
=> 1,
'submitTimes'
=> 0,
'questions'
=> [],
'videos'
=> [],
'speaks'
=> [],
];
$data
=
array
();
for
(
$i
=0;
$i
<
count
(
$wholepageItemDTOList
[
'wholepageDTOList'
]);
$i
++){
//图文型
if
(
$wholepageItemDTOList
[
'wholepageDTOList'
][
$i
][
'contentType'
] == 5){
}
//视频型
if
(
$wholepageItemDTOList
[
'wholepageDTOList'
][
$i
][
'contentType'
] == 6){
for
(
$j
=0;
$j
<
count
(
$wholepageItemDTOList
[
'wholepageDTOList'
][
$i
][
'coursepageDTOList'
]);
$j
++){
$videoId
=
$wholepageItemDTOList
[
'wholepageDTOList'
][
$i
][
'coursepageDTOList'
][
$j
][
'resourceid'
];
if
(
$videoId
!= null){
$videos
[] = [
//视频ID
"videoid"
=>
$videoId
,
//观看进度
"current"
=> 100,
//状态
"status"
=> 1,
//观看长度
"recordTime"
=> 1000,
//视频长度
"time"
=>
''
];
}
}
$temple
[
'videos'
] =
$videos
;
}
//测验题
if
(
$wholepageItemDTOList
[
'wholepageDTOList'
][
$i
][
'contentType'
] == 7){
echo
"检测到题目,开始自动答题\n"
;
$questions
= check_answer(
$wholepageItemDTOList
[
'wholepageDTOList'
][
$i
][
'coursepageDTOList'
]);
$temple
[
'questions'
] =
$questions
;
}
//设置当前学习页面id
$temple
[
'pageid'
] =
$wholepageItemDTOList
[
'wholepageDTOList'
][
$i
][
'relationid'
];
$data
[
$i
] =
$temple
;
}
$cryptoJsonData
[
'pageStudyRecordDTOList'
] =
$data
;
$cryText
= openssl_encrypt (json_encode(
$cryptoJsonData
),
'des-ecb'
, 12345678);
$params
=
array_merge
(
$public_param
, [
'data'
=>
$cryText
]);
$params
[
'header'
][] =
'Content-Type: text/plain'
;
$json
= curl(
"https://api.ulearning.cn/yws/api/personal/sync?courseType=4&platform=PC"
,
'POST'
,
$params
);
if
(
$json
!= 1){
print_r(
$params
);
die
;
}
echo
"执行状态码:$json"
.
",等待下一条命令执行\n"
;
}
//自动答题
function
check_answer(
$coursepageDTOList
)
{
global
$public_param
;
$questions
=
array
();
for
(
$i
=0;
$i
<
count
(
$coursepageDTOList
);
$i
++) {
for
(
$j
=0;
$j
<
count
(
$coursepageDTOList
[
$i
][
'questionDTOList'
]);
$j
++) {
$questionid
=
$coursepageDTOList
[
$i
][
'questionDTOList'
][
$j
][
'questionid'
];
$answerJson
= curl(
"https://api.ulearning.cn/questionAnswer/"
.
$questionid
,
'GET'
,
$public_param
);
$answerData
= json_decode(
$answerJson
, true);
$subQuestionAnswerDTOList
=
$answerData
[
'subQuestionAnswerDTOList'
];
for
(
$k
=0;
$k
<
count
(
$subQuestionAnswerDTOList
);
$k
++) {
$questions
[
$k
][
'questionid'
] =
$subQuestionAnswerDTOList
[
$k
][
'questionid'
];
$questions
[
$k
][
'answerList'
] =
$subQuestionAnswerDTOList
[
$k
][
'correctAnswerList'
];
$questions
[
$k
][
'score'
] = 100;
}
}
}
return
$questions
;
}
function
curl(
$url
,
$method
=
'GET'
,
$params
=
array
())
{
$ch
= curl_init();
//初始化 curl
$header
=
empty
(
$params
[
'header'
]) ? 0 :
$params
[
'header'
];
$ResponseHeaders
=
$params
[
'ResponseHeaders'
] ?:false;
$NOBODY
=
$params
[
'NOBODY'
] ?:false;
curl_setopt(
$ch
, CURLOPT_URL,
$url
);
//要访问网页 URL 地址
curl_setopt(
$ch
, CURLOPT_RETURNTRANSFER, true);
//返回字符串,而非直接输出到屏幕上
curl_setopt(
$ch
, CURLOPT_CONNECTTIMEOUT, false);
//连接超时时间,设置为 0,则无限等待
curl_setopt(
$ch
, CURLOPT_TIMEOUT, 3600);
//数据传输的最大允许时间超时,设为一小时
curl_setopt(
$ch
, CURLOPT_SSL_VERIFYPEER, false);
//不检查 SSL 证书来源
curl_setopt(
$ch
, CURLOPT_SSL_VERIFYHOST, false);
//不检查 证书中 SSL 加密算法是否存在
curl_setopt(
$ch
, CURLOPT_FOLLOWLOCATION, true);
//跟踪爬取重定向页面
curl_setopt(
$ch
, CURLOPT_AUTOREFERER, true);
//当Location:重定向时,自动设置header中的Referer:信息
curl_setopt(
$ch
, CURLOPT_ENCODING,
''
);
//解决网页乱码问题
curl_setopt(
$ch
, CURLOPT_HTTPHEADER,
$header
);
curl_setopt(
$ch
, CURLINFO_HEADER_OUT, true);
curl_setopt(
$ch
, CURLOPT_HEADER,
$ResponseHeaders
);
//获取响应头
curl_setopt(
$ch
, CURLOPT_NOBODY,
$NOBODY
);
//不需要内容
if
(
$method
==
'POST'
){
curl_setopt(
$ch
, CURLOPT_POST, true);
//设置为 POST
curl_setopt(
$ch
, CURLOPT_POSTFIELDS,
$params
[
"data"
]);
//提交数据
}
$data
= curl_exec(
$ch
);
//运行 curl,请求网页并返回结果
curl_close(
$ch
);
//关闭 curl
return
$data
;
}