阿里云盘定时自动签到PHP脚本

17045345632024010609492311

阿里云定时自动签到PHP脚本,本文的代码都是仿照大佬门的脚本,此内容是基于学术研究和学习目的。PHP版本亲测7.2可用 其它版本按理都是通用的,使用宝塔定时访问URL就行了。RefreshToken获取方法可以看一下下面的文章

如何获取阿里云盘的refresh_token-裕网云资源库
<?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
         * [url=home.php?mod=space&uid=952169]@Param[/url] string $url    请求的链接
         * @param mixed  $params 传递的参数
         * @param string $method 请求的方法
         * @param array  $header 请求头
         * [url=home.php?mod=space&uid=155549]@Return[/url] 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("你的Token")->run();
© 版权声明
THE END
点赞685 分享
相关推荐
评论 抢沙发
头像
善语结良缘,恶语伤人心!
提交
头像

昵称

取消
昵称表情图片

    暂无评论内容