从本地主机调用Google API

问题描述 投票:1回答:1

我正在尝试从本地Docker容器中调用Google CSE Api。显然,这因此不起作用。

为了防止SSL证书验证,我已将CURLOPT_SSL_VERIFYPEER定义为false,但是没有成功。

[如果有人对此有任何想法,将不胜感激。

我的代码:

// Create the google api client
$googleClient = new Google_Client();

// Set the google developer api key
$googleClient->setApplicationName('GoogleApi_Search');
$googleClient->setDeveloperKey(self::GOOGLE_SEARCH_API_KEY);

// for development purposes
$guzzleConfig = [
    'curl'    => [CURLOPT_SSL_VERIFYPEER => false],
    'headers' => ['Referer' => 'localhost:8080'],
];
$guzzleClient = new Client($guzzleConfig);
$googleClient->setHttpClient($guzzleClient);

// The google custom search service client
$this->googleService = new Google_Service_Customsearch($googleClient);

// Define the search parameters
$this->searchParams = [
    'cx'    => self::GOOGLE_SEARCH_ENGINE_ID,   // Custom search engine identifier
    'gl'    => 'en',                            // Location of results
    'lr'    => 'lang_en',                       // Language of results
    'num'   => 10,                              // Number of results (max. 10)
    'start' => 0,                               // The current index (max. 10)
];
php google-api google-api-php-client google-custom-search
1个回答
1
投票

我通过将start参数设置为1而不是0解决了我的问题。显然,将其设置为0会触发服务器端的致命错误,从而导致错误400 Invalid Value,并且没有其他信息。

很奇怪,但是很正常。

© www.soinside.com 2019 - 2024. All rights reserved.