QuanqiusouApi.php 6.4 KB
<?php


namespace App\Helper;

use App\Utils\HttpUtils;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\Cache;


/**
 * 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, 24 * 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){
        $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 {
                $res = HttpUtils::get($api_url, ['apino' => $api_no]);
                if($res){
                    $res = Arr::s2a($res);
                    Cache::put($key, $res, 24 * 3600);
                }
            } catch (\Exception | GuzzleException $e) {
                errorLog('获取站点收录页面数', [], $e);
                return false;
            }
        }
        return $res;
    }


    /**
     * 获取谷歌排名数据
     * @param $api_no
     * @param int $lang
     * @param int $day
     * @return array|false|mixed
     * @author zbj
     * @date 2023/5/11
     */
    public function getGoogleRank($api_no, int $lang = 0, int $day = 7)
    {
        $key = "quanqiusou_api_rank_{$api_no}_{$lang}_{$day}_" . date('Y-m-d');
        $res = Cache::get($key);
        if (!$res) {
            $param = [
                'key' => '289c1fc81c89d79c04ed4fd72822948e',
                'w' => $api_no,
                'type' => $day
            ];
            if ($lang) {
                $param['lang'] = $lang;
            }

            $api_url = $this->url . '/api';

            try {
                $res = HttpUtils::get($api_url, $param);
                if($res){
                    $res = Arr::s2a($res);
                    Cache::put($key, $res, 24 * 3600);
                }
            } catch (\Exception | GuzzleException $e) {
                errorLog('获取谷歌排名数据失败', $api_no, $e);
                return false;
            }
        }

        return $res;
    }

    /**
     * 获取每周谷歌排名数据
     * @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, 24 * 3600);
                }
            } catch (\Exception | GuzzleException $e) {
                errorLog('获取每周谷歌排名数据失败', [], $e);
                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, 24 * 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, 24 * 3600);
                }
            } catch (\Exception | GuzzleException $e) {
                errorLog('获取项目小语种数据失败', [], $e);
                return false;
            }
        }
        return $res;
    }


}