Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

masterofweb's avatar

Google Search Console API in Laravel 10

Hi I'm still quite new to Laravel development, but for my 3rd project I'm trying to build a SEO management tool for myself. For this, I need reporting data from Google Analytics and Google Search Console. For GA I'm using https://github.com/spatie/laravel-analytics and it works like a charm, thanks to the perfect documentation. Just needed to install and provide my service account credential file.

But I have trouble with GSC. The only well documented package I could find is https://github.com/schulzefelix/laravel-search-console but does not seem to be Laravel 10 compatible. All the other packages or tutorials I found where to complicated for me as a beginner, especially with all the different ways to authenticate with Googles services.

Has anyone worked with Search Console before? What is the easiest way for me to get the data into my project?

0 likes
2 replies
LaryAI's avatar
Level 58

One solution could be to use the official Google API client library for PHP and integrate it into your Laravel project. Here are the steps to do so:

  1. Install the Google API client library for PHP using Composer:
composer require google/apiclient:^2.0
  1. Create a new project in the Google Cloud Console and enable the Search Console API for it.

  2. Create a service account and download the JSON key file.

  3. In your Laravel project, create a new file config/google.php with the following contents:

<?php

return [
    'application_name' => env('GOOGLE_APPLICATION_NAME'),
    'service_account_credentials_json' => env('GOOGLE_SERVICE_ACCOUNT_CREDENTIALS_JSON'),
    'scopes' => [
        'https://www.googleapis.com/auth/webmasters.readonly',
    ],
];
  1. Add the following environment variables to your .env file:
GOOGLE_APPLICATION_NAME=Your Application Name
GOOGLE_SERVICE_ACCOUNT_CREDENTIALS_JSON=/path/to/your/keyfile.json
  1. Create a new controller method to fetch data from the Search Console API:
use Google\Client;
use Google\Service\Webmasters;

public function searchConsoleData()
{
    $client = new Client();
    $client->setApplicationName(config('google.application_name'));
    $client->setAuthConfig(config('google.service_account_credentials_json'));
    $client->setScopes(config('google.scopes'));

    $webmastersService = new Webmasters($client);
    $siteUrl = 'https://example.com'; // Replace with your site URL
    $startDate = '2022-01-01'; // Replace with your desired start date
    $endDate = '2022-01-31'; // Replace with your desired end date

    $searchAnalyticsQueryRequest = new Webmasters_SearchAnalyticsQueryRequest();
    $searchAnalyticsQueryRequest->setStartDate($startDate);
    $searchAnalyticsQueryRequest->setEndDate($endDate);
    $searchAnalyticsQueryRequest->setDimensions(['query']);
    $searchAnalyticsQueryRequest->setRowLimit(10);

    $searchAnalyticsQueryResponse = $webmastersService->searchanalytics->query($siteUrl, $searchAnalyticsQueryRequest);

    $rows = $searchAnalyticsQueryResponse->getRows();

    // Do something with the data
}

This method fetches the top 10 search queries for your site between the specified dates. You can modify the parameters to fetch different data.

  1. Call the searchConsoleData method from a route or a command in your Laravel project.

Note: This solution assumes that you are using Laravel 5.5 or higher, which includes automatic package discovery. If you are using an older version of Laravel, you may need to manually register the Google API client library service provider and facade.

willyadda's avatar

I have followed the steps in this thread but it returns the following error

{ "error": { "code": 401, "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential.

I have generated other credentials to use in a python script and it works without any problem but with Laravel I can't get it to work... any suggestions?

Please or to participate in this conversation.