本帖最后由 翼国游者 于 2023-12-16 15:06 编辑
用PHP实现阿里云签到脚本源码声明:本文的代码都是仿照大佬门的脚本 播此内容是基于学术研究和学习目的。 最近看到很多小伙伴要一个php的签到脚本 今天他来了 <?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();
PHP版本亲测7.2可用 其它版本按理都是通用的 RefreshToken获取方法网上一堆我就不重复了 (本人php小白 大佬们轻点 温柔点 那里不可以!!! 也不是不行 就一次哦)
------------------------------------------------------------------------------------ 问:refresh_token填哪? 答:在脚本的最后(new AlyPanAutoSign)->setRefreshToken("你的refresh_token")->run(); 这里填入你的refresh_token。
注:若转载请注明大神论坛来源(本贴地址)与作者信息。
|