liujingxing 2020-07-03 09:56:45 2815次浏览 4条回复 1 2 0
<?php

class YiiChina
{
    /**
     * cookie 信息
     * @var string
     */
    private $cookie = '';

    /**
     * 请求地址
     *
     * @var string
     */
    private $url = 'https://www.yiichina.com/';

    /**
     * @var null curl 信息
     */
    private $curlInfo = null;

    public function __construct($cookie)
    {
        $this->cookie = $cookie;
    }

    /**
     * 签到
     * @return array
     */
    public function registration()
    {
        return $this->sendRequest('POST', 'ajax/registration', [], [
            CURLOPT_HTTPHEADER => [
                'X-Requested-With: XMLHttpRequest',
                'X-Prototype-Version: 1.5.0',
            ],
        ]);
    }

    /**
     * 发布说说
     * @param string $content 发布内容
     * @return array
     */
    public function about($content)
    {
        // 发送请求
        return $this->sendRequest('POST', '', ['Feed[content]' => $content]);
    }

    /**
     * 获取 csrf 信息
     * @return array
     */
    public function getCsrfInfo()
    {
        $response = $this->call('GET', '');
        if (!$this->checkSuccess($response)) {
            return [
                'code' => 501,
                'msg'  => '获取CSRF信息失败',
                'data' => $response,
            ];
        }

        $content = $response['data'];
        // 获取 csrf 参数
        if (!preg_match('/\<meta name=\"csrf-param\" content=\"(.*?)\"\>/', $content, $array)) {
            return [
                'code' => 501,
                'msg'  => '获取CSRF-PARAM败',
                'data' => $content,
            ];
        }

        $csrf_param = $array[1];

        // 获取 csrf token
        if (!preg_match('/\<meta name=\"csrf-token\" content=\"(.*?)\"\>/', $content, $array)) {
            return [
                'code' => 501,
                'msg'  => '获取CSRF-TOKEN失败',
                'data' => $content,
            ];
        }

        $csrf_token = $array[1];
        return [
            'code' => 200,
            'msg'  => 'success',
            'data' => [
                'csrf_param' => $csrf_param,
                'csrf_token' => $csrf_token,
            ],
        ];
    }

    /**
     * 验证请求是否成功
     * @param mixed $response 响应内容
     * @return bool
     */
    public function checkSuccess($response)
    {
        return isset($response['code']) && $response['code'] === 200;
    }

    /**
     * 发送请求
     * @param string $method  请求的方式
     * @param string $api     请求的API
     * @param array  $data    请求的数据
     * @param array  $options 附加配置
     * @return array
     */
    public function sendRequest($method, $api, $data = [], $options = [])
    {
        // 先获取csrf参数信息
        $response = $this->getCsrfInfo();
        if (!$this->checkSuccess($response)) {
            return $response;
        }

        // 补充信息
        $csrf_token        = $response['data']['csrf_token'];
        $csrf_param        = $response['data']['csrf_param'];
        $data[$csrf_param] = $csrf_token;

        // 发送请求
        return $this->call($method, $api, $data, $options);
    }

    /**
     * 发送请求
     * @param string $method  请求的方式
     * @param string $api     请求的API
     * @param array  $data    请求的数据
     * @param array  $options 附加配置
     * @return array
     */
    public function call($method, $api, $data = [], $options = [])
    {
        $this->curlInfo = null;

        // 设置cookie
        if (!isset($options[CURLOPT_COOKIE])) {
            $options[CURLOPT_COOKIE] = $this->cookie;
        }

        $response = $this->curl($method, $this->url . ltrim($api), $data, $options);

        // 请求成功
        if (isset($response['code']) && $response['code'] === 0) {
            return [
                'code' => 200,
                'msg'  => 'success',
                'data' => $response['result'],
            ];
        }

        // 失败
        return [
            'code' => 500,
            'msg'  => 'CURL ERROR',
            'data' => [
                'error_code' => $response['code'],
                'error_msg'  => $response['error'],
            ],
        ];
    }

    /**
     * curl 处理
     * @param string $method  请求方法
     * @param string $url     请求地址
     * @param array  $params  参数
     * @param array  $options curl options 配置
     * @return array
     */
    private function curl($method, $url, $params = [], $options = [])
    {
        // 默认的请求参数
        $default = [
            CURLOPT_USERAGENT      => 'Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1)',   // 用户访问代理 User-Agent
            CURLOPT_HEADER         => 0,
            CURLOPT_FOLLOWLOCATION => 0,                   // 跟踪301
            CURLOPT_RETURNTRANSFER => 1,                   // 返回结果
            CURLOPT_IPRESOLVE      => CURL_IPRESOLVE_V4,   // 默认使用IPV4
            CURLOPT_CUSTOMREQUEST  => strtoupper($method), // 请求方法
            CURLOPT_URL            => $url,
            CURLOPT_TIMEOUT        => 30,
        ];

        // 请求参数
        if ($params) {
            $default[CURLOPT_POSTFIELDS] = is_array($params) ? http_build_query($params) : $params;
        }

        // 设置其他的配置
        if ($options) {
            foreach ($options as $key => $value) {
                $default[$key] = $value;
            }
        }

        $curl = curl_init();
        curl_setopt_array($curl, $default);
        $result = curl_exec($curl);
        $code   = curl_errno($curl);
        $error  = curl_error($curl);
        $info   = curl_getinfo($curl);

        $this->curlInfo = compact('result', 'code', 'error', 'info');
        return $this->curlInfo;
    }

}

// 实例化对象
$yiiChina = new YiiChina('传入你的cookie'); // 登录后、可以在浏览器调试模式下看的

$contents = [
    '夏虫不可语冰',
    '山有木兮木有枝,心悦君兮君不知',
    '十年生死两茫茫,不思量,自难忘',
    '曾经沧海难为水,除却巫山不是云',
    '我住长江头,君住长江尾。日日思君不见君,共饮长江水',
];

// 发布说说
$index    = mt_rand(1, count($contents)) - 1;
$response = $yiiChina->about($contents[$index]);

// 自动签到
$response = $yiiChina->registration();
echo $response;

使用定时任务执行脚本

希望不要被群主查水表

觉得很赞
您需要登录后才可以回复。登录 | 立即注册