|
| 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