评论

收藏

[PHP] 阿里云盘自动签到脚本

开发技术 开发技术 发布于:2024-03-10 00:05 | 阅读数:233 | 评论:0

阿里云盘自动签到脚本,在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();
使用方法:在代码最后一行(new AlyPanAutoSign)->setRefreshToken里填入你refresh_token就可以了,然后在PHP环境下(如宝塔面板里)设置一个计划任务每天自动执行即可。

阿里云盘获取refresh_token方法:https://www.codeae.com/thread-40588-1-1.html

关注下面的标签,发现更多相似文章