QuanqiusouApi.php 10.2 KB
<?php


namespace App\Helper;

use App\Models\RankData\RankDataLog;
use App\Utils\HttpUtils;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;


/**
 * Class QuanqiusouApi
 * @package App\Helper
 * @author zbj
 * @date 2023/5/11
 */
class QuanqiusouApi
{
    //接口地址
    protected $url = 'http://api.quanqiusou.cn';

    /**
     * 所有站点收录页面数
     * @author zbj
     * @date 2023/5/11
     */
    public function getSiteRes()
    {
//        $key = 'quanqiusou_api_site_res_' . date('Y-m-d');
//        $res = Cache::get($key);
//        if (!$res) {
            $api_url = $this->url . '/google-rank/echo_site_res.php';
            try {
                $res = HttpUtils::get($api_url, []);
                if($res){
                    $res = Arr::s2a($res);
//                    Cache::put($key, $res, 2 * 3600);
                }
            } catch (\Exception | GuzzleException $e) {
                errorLog('获取站点收录页面数', [], $e);
                return false;
            }
//        }
        return $res;
    }

    /**
     * 指定站点收录页面数
     * @param $api_no
     * @return array|false|mixed
     * @author zbj
     * @date 2023/5/11
     */
    public function getSiteResPer($api_no, $is_ext = 0, $copy_domain = ''){
//        $key = 'quanqiusou_api_site_res_per_' . $api_no . '_' . date('Y-m-d');
//        $res = Cache::get($key);
//        if (!$res) {
            $api_url = $this->url . '/google-rank/echo_site_res_per.php';
            try {
                $param = ['apino' => $api_no];
                $is_ext && $param['is_ext'] = $is_ext;
                $copy_domain && $param['domain'] = $copy_domain;
                $res = HttpUtils::get($api_url, $param);
                if($res){
                    $res = Arr::s2a($res);
//                    Cache::put($key, $res, 2 * 3600);
                }
            } catch (\Exception | GuzzleException $e) {
                errorLog('获取站点收录页面数', [], $e);
                return false;
            }
//        }
        return $res;
    }

    /**
     * 复制站点
     * @return array|false|mixed
     * @author zbj
     * @date 2023/5/11
     */
    public function getSiteNumList(){
        $api_url = $this->url . '/google-rank/ext_sitenum_list.php';
        try {
            $res = HttpUtils::get($api_url, []);
            if($res){
                $res = Arr::s2a($res);
            }
        } catch (\Exception | GuzzleException $e) {
            errorLog('获取复制站点收录页面数', [], $e);
            return [];
        }
        return array_column($res, 'url');
    }


    /**
     * 获取谷歌排名数据
     * @param $project_id
     * @param $api_no
     * @param string $lang
     * @param int $day
     * @return array|false|mixed
     * @author zbj
     * @date 2023/5/11
     */
    public function getGoogleRank($project_id, $api_no, string $lang = '', int $day = 7, $force = true)
    {
        $key = "quanqiusou_api_rank_{$api_no}_{$lang}_{$day}_" . date('Y-m-d');
        $res = Cache::get($key);
        if (!$res || $force) {
            if ($lang) {
                $api_no = $api_no . '_' . $lang;
            }
            $today = date('Y-m-d');
            $startDay = date('Y-m-d', strtotime('-'.$day.' day'));
            $endDay = date('Y-m-d', strtotime('-1 day'));
            //8918_kr_2024-02-19_2024-02-25.json  8918_2024-02-19_2024-02-25.json
            $api_url = "https://quanqiusou.cn/google-rank/data_json/{$today}/{$api_no}_{$startDay}_{$endDay}.json";
            try {
 				//$res = HttpUtils::get($api_url, []);
                $client = new Client();
                $res = $client->request('GET', $api_url, [
                    'proxy' => env('CURL_PROXY'), // 代理服务器地址和端口号
                    // 其他请求选项
                ])->getBody()->getContents();
                if($res){
                    $res = Arr::s2a($res);
                    Cache::put($key, $res, 2 * 3600);
                }
            } catch (\Exception | GuzzleException $e) {
                errorLog('获取谷歌排名数据失败', [$api_no], $e);
                return false;
            }
        }

        return $res;
    }

    /**
     * 获取每周谷歌排名数据
     * fixme https://quanqiusou.cn/extend_api/api/data/2024-09-02.json
     * @return array|false|mixed
     * @author zbj
     * @date 2023/5/11
     */
    public function getGoogleRankWeek()
    {
        $key = "quanqiusou_api_week_data_" . date('Y-m-d');
        $res = Cache::get($key);
        if (!$res) {
            $api_url = $this->url . '/google-rank/echo_week_data.php';
            try {
                $res = HttpUtils::get($api_url, []);
                if($res){
                    $res = Arr::s2a($res);
                    Cache::put($key, $res, 2 * 3600);
                }
            } catch (\Exception | GuzzleException $e) {
                Log::channel('rank_data')->error('获取每周谷歌排名数据失败' . $e->getMessage());
                return false;
            }
        }
        return $res;
    }


    /**
     * 获取历史排名统计数据
     * @param $api_no
     * @param string $lang
     * @return array|false|int|mixed|null
     * @author zbj
     * @date 2023/5/15
     */
    public function getHistoryCount($api_no, $lang = '')
    {
//        $key = "quanqiusou_api_history_count_{$api_no}_{$lang}_" . date('Y-m-d');
//        $res = Cache::get($key);
//        if (!$res) {
            $api_url = $this->url . '/google-rank/history_count.php';
            $param = [
                'apino' => $api_no,
            ];
            if ($lang) {
                $param['lang'] = $lang;
            }
            try {
                $res = HttpUtils::get($api_url, $param);
                if($res){
                    $res = Arr::s2a($res);
//                    Cache::put($key, $res, 2 * 3600);
                }
            } catch (\Exception | GuzzleException $e) {
                errorLog('获取历史排名统计数据失败', [], $e);
                return false;
            }
//        }
        return $res;
    }


    /**
     * 获取小语种项目
     * @return array|false|int|mixed|null
     * @author zbj
     * @date 2023/5/15
     */
    public function getLangList()
    {
        $key = "quanqiusou_api_lang_list_" . date('Y-m-d');
        $res = Cache::get($key);
        if (!$res) {
            $api_url = $this->url . '/api/index/langlist';
            $param = [
                'key' => '289c1fc81c89d79c04ed4fd72822948e',
            ];
            try {
                $res = HttpUtils::get($api_url, $param);
                if($res){
                    $res = Arr::s2a($res);
                    Cache::put($key, $res, 2 * 3600);
                }
            } catch (\Exception | GuzzleException $e) {
                errorLog('获取小语种项目数据失败', [], $e);
                return false;
            }
        }
        return $res;
    }

    /**
     * 获取项目小语种信息
     * @return array|false|int|mixed|null
     * @author zbj
     * @date 2023/5/15
     */
    public function getLangRankData($api_no)
    {
        $key = "quanqiusou_get_language_rank_data_{$api_no}_" . date('Y-m-d');
        $res = Cache::get($key);
        if (!$res) {
            $api_url = $this->url . '/api/index/get_language_rank_data';
            $param = [
                'apino' => $api_no,
            ];
            try {
                $res = HttpUtils::get($api_url, $param);
                if($res){
                    $res = Arr::s2a($res);
                    Cache::put($key, $res, 2 * 3600);
                }
            } catch (\Exception | GuzzleException $e) {
                errorLog('获取项目小语种数据失败', [], $e);
                return false;
            }
        }
        return $res;
    }

    /**
     * 项目api_no对应关系
     * @return array|false|int|mixed|null
     * @author zbj
     * @date 2023/10/16
     */
    public function getWebApiNo(){
        $key = "quanqiusou_web_api_no_" . date('Y-m-d');
        $res = Cache::get($key);
        if (!$res) {
            $api_url = $this->url . '/api/index/weblist';
            $param = [
                'key' => '289c1fc81c89d79c04ed4fd72822948e',
            ];
            try {
                $res = HttpUtils::get($api_url, $param);
                if($res){
                    $res = Arr::s2a($res);
                    Cache::put($key, $res, 2 * 3600);
                }
            } catch (\Exception | GuzzleException $e) {
                errorLog('获取项目API_NO数据失败', [], $e);
                return false;
            }
        }
        return $res;
    }

    /**
     * 获取代理信息
     * @param $domain
     * @return array|mixed
     * @author zbj
     * @date 2024/10/26
     */
    public function getV5Agent($domain){
        $token = md5($domain.'qqs');
        try {
            $client = new \GuzzleHttp\Client();
            $res = $client->request('GET', 'https://quanqiusou.cn/extend_api/api/get_agent_by_domain.php?'.http_build_query(['token' => $token, 'domain' => $domain]), [
                'proxy' => env('CURL_PROXY'), // 代理服务器地址和端口号
            ])->getBody()->getContents();
            return Arr::s2a($res);
        } catch (\Exception | GuzzleException $e) {
            errorLog('获取代理失败', [$domain], $e);
            return [];
        }
    }

    /**
     * 获取代理信息
     * @param $domain
     * @return array|mixed
     * @author zbj
     * @date 2024/10/26
     */
    public function getV5RemainDay($domain){
        $token = md5($domain.'qqs');
        try {
            $client = new \GuzzleHttp\Client();
            $res = $client->request('GET', 'https://quanqiusou.cn/extend_api/api/get_remain_day_by_domain.php?'.http_build_query(['token' => $token, 'domain' => $domain]), [
                'proxy' => env('CURL_PROXY'), // 代理服务器地址和端口号
            ])->getBody()->getContents();
            return Arr::s2a($res);
        } catch (\Exception | GuzzleException $e) {
            errorLog('获取代理失败', [$domain], $e);
            return [];
        }
    }

}