Skip to content

Commit 287b351

Browse files
committed
feat: google indexing api
1 parent 77933e9 commit 287b351

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"require": {
66
"php": ">=5.5.9",
77
"cakephp/cakephp": "^3.5",
8+
"google/apiclient": "^2.14",
89
"firebase/php-jwt": "~6.0",
910
"raiym/instagram-php-scraper": "^0.10.0"
1011
},
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
namespace Trois\Utils\Utility\Http;
3+
4+
use Psr\Http\Message\ServerRequestInterface;
5+
use Cake\Core\Configure;
6+
use Cake\Utility\Inflector;
7+
use Cake\Utility\Hash;
8+
9+
class GoogleIndexingApi {
10+
11+
/*
12+
REFs:
13+
- https://developers.google.com/search/apis/indexing-api/v3/prereqs?hl=en#php
14+
- https://developers.google.com/search/apis/indexing-api/v3/using-api?hl=en
15+
- https://googleapis.github.io/google-api-php-client/main/Google/Client.html#method_setAuthConfig
16+
17+
*/
18+
19+
protected $endpoint = 'https://indexing.googleapis.com/v3/urlNotifications:publish';
20+
protected $scope = 'https://www.googleapis.com/auth/indexing';
21+
22+
public function updateUrl($url){
23+
24+
$client = new \Google_Client();
25+
26+
$authConfig = Configure::read('ApiKeys.Google-indexing');
27+
$client->setAuthConfig($authConfig);
28+
$client->addScope($this->scope);
29+
30+
$httpClient = $client->authorize();
31+
32+
$content = [
33+
'url' => $url,
34+
'type' => 'URL_UPDATED'
35+
];
36+
37+
$response = $httpClient->post($this->endpoint, ['json' => $content]);
38+
39+
return $response;
40+
}
41+
42+
public function removeUrl($url)
43+
{
44+
$client = new \Google_Client();
45+
46+
$authConfig = Configure::read('ApiKeys.Google-indexing');
47+
48+
$client->setAuthConfig($authConfig);
49+
$client->addScope($this->scope);
50+
51+
$httpClient = $client->authorize();
52+
53+
$content = [
54+
'url' => $url,
55+
'type' => 'URL_DELETED'
56+
];
57+
58+
$response = $httpClient->post($this->endpoint, ['json' => $content]);
59+
60+
return $response;
61+
}
62+
}

0 commit comments

Comments
 (0)