GoogleSearchService.php 3.0 KB
<?php
/**
 * @remark :
 * @name   :GoogleSearchService.php
 * @author :lyh
 * @method :post
 * @time   :2025/3/25 11:36
 */

namespace App\Services;

use App\Helper\Country;

/**
 * @remark :google关键字扩展
 * @name   :GoogleSearchService
 * @author :lyh
 * @method :post
 * @time   :2025/3/25 11:38
 */
class GoogleSearchService
{
    public $url = "";

    /**
     * @remark :扩展关键词请求数据
     * @name   :requestUrl
     * @author :lyh
     * @method :post
     * @time   :2025/3/25 11:36
     */
    public function requestUrl($keyword){
        $this->url = 'https://google-keyword-insight1.p.rapidapi.com/globalkey';
        $url = $this->url.'/?keyword='.$keyword.'&lang=en';
        return $this->curlGoogleApi($url);
    }

    /**
     * @remark :热门关键词拉取
     * @name   :requestKeywordUrl
     * @author :lyh
     * @method :post
     * @time   :2025/3/27 16:57
     */
    public function requestKeywordUrl($keyword){
        $this->url = 'https://google-keyword-insight1.p.rapidapi.com/topkeys';
        $url = $this->url.'/?keyword='.$keyword.'&location=US&&lang=en';
        return $this->curlGoogleApi($url);
    }

    /**
     * @remark :请求
     * @name   :curlGoogleApi
     * @author :lyh
     * @method :post
     * @time   :2025/3/27 16:59
     */
    public function curlGoogleApi($url){
        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "GET",
            CURLOPT_HTTPHEADER => [
                "x-rapidapi-host: google-keyword-insight1.p.rapidapi.com",
                "x-rapidapi-key: d246239565mshc29088b58ff484dp17c0bdjsn2d28d03622c7"
            ],
        ]);
        $response = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);
        if ($err) {
            return false;
        } else {
            return json_decode($response,true);
        }
    }

    /**
     * @remark :google搜索
     * @name   :googleSearch
     * @author :lyh
     * @method :post
     * @time   :2025/3/27 11:18
     * @param  :date,query,page,device,country/时间、聚合、前页、设备、国家
     */
    public function googleSearch($domain,$search){
        $data = [1=>'date', 2=>'query', 3=>'page', 4=>'device', 5=>'country'];
        $this->url = 'https://www.cmer.site/api/google/search';
        $url = $this->url.'?domain='.$domain.'&q='.$data[$search];
        $data = http_get($url);
        if(!isset($data['status']) || $data['status'] != 200){
            return [];
        }
        $data = $data['data'] ?? [];
        if(!empty($data) && ($search == 5)){
            foreach ($data as $key => $val){
                $val['keys_country'] = Country::getCountryNameByAlpha3(strtoupper($val['keys'][0]));
                $data[$key] = $val;
            }
        }
        return $data;
    }
}