<?php
/**
https://developers.google.com/search/apis/indexing-api/v3/prereqs
+ enabling the Indexing API
+ creating a new service account (copy client_email in .json service account)
+ verifying ownership in Search Console (add above email: ie indexapi@project-id-3937556376642046437.iam.gserviceaccount.com in Verified owners section for your site in google search console).
*/
require_once 'vendor/autoload.php';
$client = new Google_Client();
$client->setApplicationName("indexapi");
$client->setAuthConfig(__DIR__.'/google-index-project-id-3937556376642046437-f70aec5f1182.json');
$client->addScope('https://www.googleapis.com/auth/indexing');
// Get a Guzzle HTTP Client
$httpClient = $client->authorize();
$endpoint = 'https://indexing.googleapis.com/v3/urlNotifications:publish';
/**
Define contents here. The structure of the content is described in the next step.
*/
$content = [
"url"=> "http://cuacuonchaua.com/",
"type"=> "URL_UPDATED"
];
$response = $httpClient->post($endpoint, [ 'body' => json_encode($content) ]);
$status_code = $response->getStatusCode();
print_r($status_code);
//update url
["type"=> "URL_UPDATED"]
//delete url
["type": "URL_DELETED"]
/*
send batch update: 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
*/
$client->setUseBatch(true);
$batch = new Google_Http_Batch($client);
//http://docs.guzzlephp.org/en/stable/ | https://developers.google.com/api-client-library/php/guide/batch
/*$request = new \GuzzleHttp\Psr7\Request('POST', $endpoint, [ 'body' => json_encode($content) ]);
$batch->add($request, 'key1');*/
//init google batch and set root URL
$batch = new Google_Http_Batch($client,false,'https://indexing.googleapis.com');
//init service Notification to sent request
$postBody = new Google_Service_Indexing_UrlNotification();
$postBody->setType('URL_UPDATED');
$postBody->setUrl('https://your_job_detail');
//init service Indexing ( like updateJobPosting )
$service = new Google_Service_Indexing($client);
//create request
//$service->urlNotifications->createRequestUri('https://indexing.googleapis.com/batch');
$request_kame = $service->urlNotifications->publish($postBody);
//add request to batch
$batch ->add($request_kame);
$result = $batch->execute();
foreach($result['response-key1'] as $item) {
echo $item['volumeInfo']['title'], "<br /> \n"; //for example
}
/*
Get notification status
{
url: “http://foo.com“
latest_update {
type: “URL_UPDATED”
notify_time: “2017-07-31T19:30:54.524457662Z”
}
latest_remove {
type: “URL_DELETED”
notify_time: “2017-08-31T19:30:54.524457662Z”
}
}
*/
$httpClient->post('https://indexing.googleapis.com/v3/urlNotifications/metadata');