【问题标题】:Calling Google API from localhost从 localhost 调用 Google API
【发布时间】:2019-12-05 17:47:07
【问题描述】:

我正在尝试从我的 localhost Docker 容器调用 Google CSE Api。显然,正因为如此,这不起作用。

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

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

我的代码:

// 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)
];

【问题讨论】:

  • "我已将 CURLOPT_SSL_VERIFYPEER 定义为 false 以防止 SSL 证书验证,但没有成功。"为什么?无论您是从本地主机还是其他地方调用 Google 的 SSL,它都应该是完全有效的。您遇到什么错误或具体问题?你给我们的只是“这行不通”。
  • 我有一个错误 400 “无效值”。服务器不返回其他信息。

标签: php google-api google-api-php-client google-custom-search


【解决方案1】:

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

奇怪但有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    • 2018-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    相关资源
    最近更新 更多