Search code examples
phpgoogle-apigoogle-api-php-clientgoogle-custom-search

Calling Google API from localhost


I am trying to call the Google CSE Api from my localhost Docker container. Apparently, this is not working because of that.

I have defined CURLOPT_SSL_VERIFYPEER to false in order to prevent SSL certificate verification, but with no success.

If anyone has any thought on this, help would be appreciated.

My code:

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

Solution

  • I solved my issue by setting the start parameter to 1 instead of 0. Apparently, setting it to be 0 trigger a fatal error on the server side which causes the error 400 Invalid Value and no other information.

    Strange but working.