Using Google Indexing API with PHP and Laravel

This article covers all the steps you need to do, so that you can use Google Indexing API and do indexing requests using PHP and PHP framework like Laravel. Using Google Indexing API is better than pinging Google about sitemap change every time you have changes on your site. You can install Google APIs client libraries using Composer or install it manually by downloading the libraries from GitHub. I also wrote code examples so that you learn how to do single requests and how to do requests in batch. Let's start!
- Verifying your domain ownership in Google Search Console
- Creating Service Account in Google Cloud Platform
- Creating private key file for Google API authentication
- Adding Google Service Account as domain property owner
- Installing Google APIs Client library for PHP
- Code examples
1. Verifying your domain ownership in Google Search Console
First you need to verify your domain at Google Search Console. Verifying means that before you can do anything, Google need to know that you really own that specific domain and have access to it.
1. Go to https://search.google.com/search-console/welcome create account and login using your Google Account.
2. Open menu from top left and click Add propety.

3. If you do not have access to your DNS records, or if you are not experienced, i suggest that you just go with URL prefix option. Just write down your url and hit Continue.

4. Choose the verification method you want to use. Easiest is HTML file. Download HTML file and upload it to root of your domain, and hit Verify -button.

5. After you have verified your domain you can continue for the next next and create Google Service Account.
2. Creating Service Account in Google Cloud Platform
Now you need to create Google Service Account and that account is used for the access of Google Indexing API.
1. Go to https://console.cloud.google.com/iam-admin/serviceaccounts.
2. Go to Service Accounts > Create Project.

3. Give name to your project and click Create.

4. Make sure that your newly created project is selected and click Create Service Account.

5. Fill in service account details and give it description so that you know where you are going to use it.

6. Change the role to Owner and hit Continue.

7. Click done.

3. Creating private key file for Google API authentication
Now we create private key file in JSON format and this JSON file is used by PHP / Laravel to access the Google API.
1. Click the service account email address.

2. Go to Keys > Add key > Create new key.

3. Select JSON and click Create, and save the JSON file to place where you find it.

4. Adding Google Service Account as domain property owner
For the last step you have to add Google Service Account as the domain propety owner in Google Search Console. After this your Service Account has permission to access to your domain properties through Google API.
1. Go to Google Search Console https://search.google.com/search-console/welcome.
2. Select your domain and go to Settings > Users and permissions.

3. Click the three dots next to your username and click Manage property owners.

4. Click Add an owner.

5. Now copy and paste your Service Account email address as your domain propety owner.

5. Installing Google APIs Client library for PHP
Next you need Google APIs Client libraries so that you don't have to write everything from the scratch. Here is link to the GitHub page and full documentation https://github.com/googleapis/google-api-php-client.
Install using Composer
You can install the libraries using composer by writing composer require google/apiclient . If you are using framework like Laravel, it should autoload the libraries for your project after composer require.
Install manually by downloading the libraries
Download the latest libraries for your PHP version here https://github.com/googleapis/google-api-php-client/releases and unzip the content to your project folder. Include the library to your project with
require_once '/path/to/google-api-php-client/vendor/autoload.php';
6. Code examples
Remember to load Google API Client libraries like in the examples above. Also when you are tinkering and testing, remember that you cannot use http://localhost or any other domains that you haven't associated with your Google Service Account. If you try to use any domains that you haven't verified and associated with your Google Service Account, you are going to get errors.
To reduce the number of HTTP connections your client has to make, you can combine up to 100 calls to the Indexing API into a single HTTP request. You do this in a multi-part request called a batch. Quota is counted at the URL level. For example, if you combine 10 requests into a single HTTP request, it still counts as 10 requests for your quota. Default quota is 200 requests per day.
Send single indexing or delete request using Google Indexing API
Use this when you want to notify Google about new page you released or a page that was updated.
use Google;
use Google_Service_Indexing;
use Google_Service_Indexing_UrlNotification;
try {
$googleClient = new Google\Client();
// Add here location to the JSON key file that you created and downloaded earlier.
$googleClient->setAuthConfig( '/location/to/key-file.json' );
$googleClient->setScopes( Google_Service_Indexing::INDEXING );
$googleIndexingService = new Google_Service_Indexing( $googleClient );
// Use URL_UPDATED for new or updated pages.
// Use URL_DELETED for deleted pages.
$urlNotification = new Google_Service_Indexing_UrlNotification([
'url' => "https://wiretuts.com/connect-playstation-3-controller-to-pc-with-vigem-and-x360ce",
'type' => 'URL_UPDATED'
]);
$result = $googleIndexingService->urlNotifications->publish( $urlNotification );
/*
Because we are using try-catch there is no real need for checking the result in the
try -block. But if you want to access the values that Google returns, below are
the examples.
$result->urlNotificationMetadata->latestUpdate["notifyTime"]; // Notification time.
$result->urlNotificationMetadata->latestUpdate["type"]; // Notification type.
$result->urlNotificationMetadata->latestUpdate["url"]; // Url that you submitted.
*/
}
catch (\Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
Send batch indexing or delete requests using Google Indexing API
Use this when you want to notify Google about multiple page changes with one request. You can batch up to 100 URLs to one request. Daily quota is 200 urls.
use Google;
use Google_Service_Indexing;
use Google_Service_Indexing_UrlNotification;
try {
$googleClient = new Google\Client();
// Add here location to the JSON key file that you created and downloaded earlier.
$googleClient->setAuthConfig( '/location/to/key-file.json' );
$googleClient->setScopes( Google_Service_Indexing::INDEXING );
$googleClient->setUseBatch( true );
$service = new Google_Service_Indexing( $googleClient );
$batch = $service->createBatch();
$postBody = new Google_Service_Indexing_UrlNotification();
// Use URL_UPDATED for new or updated pages.
// Use URL_DELETED for deleted pages.
$urls = array(
'https://wiretuts.com/connect-playstation-3-controller-to-pc-with-vigem-and-x360ce' => 'URL_UPDATED',
'https://wiretuts.com/connect-playstation-4-controller-to-pc-with-x360ce' => 'URL_UPDATED',
'https://wiretuts.com/enabling-ssh-on-raspberry-pi-os-for-remote-use' => 'URL_UPDATED'
);
foreach($urls as $url => $type)
{
$postBody->setUrl( $url );
$postBody->setType( $type );
$batch->add( $service->urlNotifications->publish( $postBody ) );
}
$results = $batch->execute();
/*
If you want to loop trough the results of the each page, you can do it with
the example below.
foreach($results as $result)
{
echo $result->urlNotificationMetadata->latestUpdate["notifyTime"] . "\n";
echo $result->urlNotificationMetadata->latestUpdate["type"] . "\n";
echo $result->urlNotificationMetadata->latestUpdate["url"] . "\n";
}
*/
}
catch (\Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
Send single indexing status request to Google Indexing API
Use this when you want to check indexing status of single page. If there isn't any information, that means that Google hasn't received any indexing requests.
use Google;
use Google_Service_Indexing;
try {
$googleClient = new Google\Client();
// Add here location to the JSON key file that you created and downloaded earlier.
$googleClient->setAuthConfig( '/location/to/key-file.json' );
$googleClient->setScopes( Google_Service_Indexing::INDEXING ) ;
$googleIndexingService = new Google_Service_Indexing( $googleClient );
$url = ["url" => "https://wiretuts.com/connect-ps3-and-ps4-controllers-to-pc-at-the-same-time"];
$result = $googleIndexingService->urlNotifications->getMetadata( $url );
/*
You can access the return values with examples below.
$result->latestUpdate->notifyTime; // Last time you notified Google about changes
$result->latestUpdate->type; // Notify type
$result->latestUpdate->url; // Notify url
*/
}
catch (\Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}/code>
Send batch indexing status request to Google Indexing API
Use this when you want to check indexing status of multiple pages as a batch. You can batch up to 100 URLs to one request. Daily quota is 200 urls.
use Google;
use Google_Service_Indexing;
try {
$googleClient = new Google\Client();
// Add here location to the JSON key file that you created and downloaded earlier.
$googleClient->setAuthConfig( '/location/to/key-file.json' );
$googleClient->setScopes( Google_Service_Indexing::INDEXING );
$googleClient->setUseBatch( true );
$service = new Google_Service_Indexing( $googleClient );
$batch = $service->createBatch();
$urls = array(
'https://wiretuts.com/connect-playstation-3-controller-to-pc-with-vigem-and-x360ce',
'https://wiretuts.com/connect-playstation-4-controller-to-pc-with-x360ce',
'https://wiretuts.com/enabling-ssh-on-raspberry-pi-os-for-remote-use'
);
foreach($urls as $url) {
$urlArray = ['url' => $url];
$batch->add( $service->urlNotifications->getMetadata( $urlArray ) );
}
$results = $batch->execute();
/*
You can access the return values with example below.
foreach($results as $result) {
echo $result->latestUpdate->notifyTime . "\r\n";
echo $result->latestUpdate->type . "\r\n";
echo $result->latestUpdate->url . "\r\n";
}
*/
}
catch (\Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
14 Comments