脚本代码
<?php
/**
* 阿里云盘自动签到
*/
class AlyPanAutoSign
{
protected $refresh_token = '';
protected $access_token = '';
protected $user_name = '';
protected $user = [];
public function __construct($refresh_token = '')
{
if ($refresh_token) $this->setRefreshToken($refresh_token);
}
/**
* CURL发送Request请求,含POST和REQUEST
* @Param string $url 请求的链接
* @param mixed $params 传递的参数
* @param string $method 请求的方法
* @param array $header 请求头
* @Return array
*/
protected function sendRequest($url, $params = [], $method = 'POST', $header = [])
{
$method = strtoupper($method);
$protocol = substr($url, 0, 5);
$query_string = is_array($params) ? http_build_query($params) : $params;
$ch = curl_init();
$defaults = [];
if ('GET' == $method) {
$geturl = $query_string ? $url . (stripos($url, "?") !== false ? "&" : "?") . $query_string : $url;
$defaults[CURLOPT_URL] = $geturl;
} else {
$defaults[CURLOPT_URL] = $url;
if ($method == 'POST') {
$defaults[CURLOPT_POST] = 1;
} else {
$defaults[CURLOPT_CUSTOMREQUEST] = $method;
}
$defaults[CURLOPT_POSTFIELDS] = $params;
}
$defaults[CURLOPT_HEADER] = false;
$defaults[CURLOPT_USERAGENT] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.98 Safari/537.36";
$defaults[CURLOPT_FOLLOWLOCATION] = true;
$defaults[CURLOPT_RETURNTRANSFER] = true;
$defaults[CURLOPT_CONNECTTIMEOUT] = 3;
$defaults[CURLOPT_TIMEOUT] = 3;
// disable 100-continue
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
if ('https' == $protocol) {
$defaults[CURLOPT_SSL_VERIFYPEER] = false;
$defaults[CURLOPT_SSL_VERIFYHOST] = false;
}
curl_setopt_array($ch, $defaults);
$ret = curl_exec($ch);
$err = curl_error($ch);
if (false === $ret || !empty($err)) {
$errno = curl_errno($ch);
$info = curl_getinfo($ch);
curl_close($ch);
return [
'ret' => false,
'errno' => $errno,
'msg' => $err,
'info' => $info,
];
}
curl_close($ch);
return [
'ret' => true,
'msg' => $ret,
];
}
protected function writeln($msg)
{
if (PHP_SAPI == 'cli') {
echo "$msg\n";
} else {
echo "$msg </br>";
}
}
protected function error($msg)
{
if (PHP_SAPI == 'cli') {
echo "<error>$msg</error>\n";
} else {
echo "<span style='color: red'>$msg</span><br>";
}
die;
}
protected function success($msg)
{
if (PHP_SAPI == 'cli') {
echo "<success>$msg</success>\n";
} else {
echo "<span style='color: green'>$msg</span><br>";
}
die;
}
public function setRefreshToken($refresh_token = '')
{
$this->refresh_token = $refresh_token;
return $this;
}
/**
* 获取用户信息
*/
protected function getAccessToken()
{
try {
$res = $this->sendRequest("https://auth.aliyundrive.com/v2/account/token", json_encode([
'grant_type' => 'refresh_token',
'refresh_token' => $this->refresh_token,
]), 'POST', ['Content-Type: application/json',]);
if (!$res['ret']) $this->error("获取access_token失败");
$data = json_decode($res['msg'], true);
if (isset($data['message'])) $this->writeln($data['message']);
if (!isset($data['access_token'])) $this->error("refresh_token异常,获取access_token失败 可能是refresh_token过期");
$this->access_token = $data['access_token'];
$this->user_name = $data['nick_name'] . ":" . $data['user_name'];
$this->user = $data;
$this->writeln($this->user_name . " ---登录成功---");
} catch (Exception $e) {
$this->error("程序错误:" . $e->getMessage());
}
}
/**
* 签到
*/
protected function signIn()
{
if (!$this->user) $this->error("请登录!!!");
try {
$res = $this->sendRequest("https://member.aliyundrive.com/v1/activity/sign_in_list", json_encode([
"_rx-s" => "mobile",
]), 'POST', ['Content-Type: application/json', "Authorization: Bearer $this->access_token"]);
if (!$res['ret']) $this->error("请求签到失败");
$data = json_decode($res['msg'], true);
if (isset($data['message'])) $this->writeln($data['message']);
if (!isset($data['result']['signInCount'])) $this->error("签到失败");
$this->writeln("---$this->user_name 签到成功,本月累计签到{$data['result']['signInCount']}次---");
$this->user['signInCount'] = $data['result']['signInCount'];
} catch (Exception $e) {
$this->error("程序错误:" . $e->getMessage());
}
}
/**
* 领取签到奖励
*/
protected function receiveSignInReward()
{
if (!$this->user) $this->error("请登录!!!");
try {
$res = $this->sendRequest("https://member.aliyundrive.com/v1/activity/sign_in_reward?_rx-s=mobile'", json_encode([
'signInDay' => $this->user['signInCount'],
]), 'POST', ['Content-Type: application/json', "Authorization: Bearer $this->access_token"]);
if (!$res['ret']) $this->error("请求获取奖励失败");
$data = json_decode($res['msg'], true);
if (isset($data['message'])) $this->writeln($data['message']);
if (!isset($data['result']['notice'])) $this->error("签到失败");
$this->writeln("---$this->user_name 领取奖励成功,{$data['result']['name']}-{$data['result']['description']}-{$data['result']['notice']}---");
} catch (Exception $e) {
$this->error("程序错误:" . $e->getMessage());
}
}
public function run($refresh_token = '')
{
if ($refresh_token) $this->refresh_token = $refresh_token;
$this->getAccessToken();
$this->signIn();
$this->receiveSignInReward();
}
}
(new AlyPanAutoSign)->setRefreshToken("你的refresh_token")->run();
懒人一键下载脚本:https://www.123pan.com/s/8pFiVv-pTbUh.html
使用方法
1.获取阿里云refresh_token,用自己帐号登录网页版云盘,按照下面方法获取。
2.在脚本代码最后(new AlyPanAutoSign)->setRefreshToken("你的refresh_token")->run(); 这里填入你的refresh_token
3.在宝塔计划任务设置每日定时访问脚本即可。
4.签到成功示例。
常见问题
如遇签到失败,绝大部分原因是因为refresh_token失效(一般有效期一个月),只要按上面方法重新获取refresh_token并更新即可。
© 版权声明
ZUITX.COM
暂无评论内容