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

namespace App\Services;

use App\Helper\Country;

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

    public $key = '3eba1ba999msh3a7c11101a7e298p19924bjsn5089487f7c37';

    /**
     * @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';
        $param = [
            'keyword'=>$keyword,
            'location'=>'US',
            'lang'=>'en'
        ];
        $query_string = str_replace('+', '%20', http_build_query($param));
        $url = $this->url.'/?'.$query_string;
        $data = $this->curlGoogleApi($url);
        return $data;
    }

    /**
     * @remark :备用数据
     * @name   :keywordResearch
     * @author :lyh
     * @method :post
     * @time   :2025/4/10 10:23
     */
    public function keywordResearch($keyword){
        $this->url = 'https://seo-keyword-research.p.rapidapi.com/keynew.php';
        $param = ["keyword" => $keyword, 'country' => 'us'];
        $query_string = str_replace('+', '%20', http_build_query($param));
        $url = $this->url.'/?'.$query_string;
        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: $this->key"
            ],
        ]);
        $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){
        $this->url = 'https://www.cmer.site/api/google/search';
        $url = $this->url.'?domain='.$domain.'&q='.$search;
        $data = http_get($url);
        if(!isset($data['status']) || $data['status'] != 200){
            return [];
        }
        $data = $data['data'] ?? [];
        return $data;
    }
}