Skip to content

Commit

Permalink
Добавлен google/apiclient
Browse files Browse the repository at this point in the history
  • Loading branch information
dllpl committed Jan 11, 2024
1 parent b64e1d0 commit bea2c11
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 74 deletions.
13 changes: 10 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@
],
"require": {
"php": "^7.4|| ^8.0",
"firebase/php-jwt": "^6.10",
"guzzlehttp/guzzle": "^7.8",
"ext-json": "*"
"ext-json": "*",
"google/apiclient": "2.15.0"
},
"scripts": {
"pre-autoload-dump": "Google\\Task\\Composer::cleanup"
},
"extra": {
"google/apiclient-services": [
"Indexing"
]
}
}
98 changes: 27 additions & 71 deletions src/FastIndexing.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,93 +2,49 @@

namespace Dllpl\Google;

use Firebase\JWT\JWT;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;

final class FastIndexing
{
/** @var Client */
private Client $client;
/** @var \Google_Client */
private \Google_Client $client;

/** @var string */
private string $jwt;
/** @var \Google_Service_Indexing */
private \Google_Service_Indexing $service;

/**
* @param string $keyFile json файл из личного аккаунта разработчика Google
* @throws \Exception
*/
public function __construct(string $keyFile)
{
$this->client = new Client();
return $this->jwt = $this->makeJWT($keyFile);
}
/** @var \Google_Service_Indexing_UrlNotification */
private \Google_Service_Indexing_UrlNotification $postBody;

/**
* @param string $batchFile
* @return string
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Exception
* @param string $keyPath
* @throws \Google\Exception
*/
public function send(string $batchFile): string
public function __construct(string $keyPath)
{
$batch = explode("\n", file_get_contents($batchFile));
$this->client = new \Google_Client();
$this->client->setAuthConfig($keyPath);
$this->client->addScope('https://www.googleapis.com/auth/indexing');
$this->client->setUseBatch(true);

$items = array_map(function ($line) {
return [
'Content-Type' => 'application/http',
'Content-ID' => '',
'body' =>
"POST /v3/urlNotifications:publish HTTP/1.1\n" .
"Content-Type: application/json\n\n" .
json_encode([
'url' => $line,
'type' => 'URL_UPDATED'
])
];
}, $batch);
$this->service = new \Google_Service_Indexing($this->client);

try {
$response = $this->client->request('POST', 'https://indexing.googleapis.com/batch', [
'headers' => [
'Content-Type' => 'multipart/mixed',
'Authorization' => 'Bearer ' . $this->jwt
],
'json' => $items
]);
$this->postBody = new \Google_Service_Indexing_UrlNotification();

return $response->getBody()->getContents();
} catch (ClientException $exception) {
return throw new \Exception([
'response' => $exception->getResponse()->getBody()->getContents(),
'exception' => $exception->getMessage()
]);
}
}

/**
* @param string $keyFile
* @return string
* @throws \Exception
* @param string $batchFile
* @return array
*/
private function makeJWT(string $keyFile): string
public function send(string $batchFile): array
{
$key = json_decode(file_get_contents($keyFile), true);
$file = file($batchFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

if (isset($key['client_email'], $key['private_key'])) {
try {
return JWT::encode([
'iss' => $key['client_email'],
'scope' => 'https://www.googleapis.com/auth/indexing',
'aud' => 'https://oauth2.googleapis.com/token',
'exp' => time() + 3600,
'iat' => time(),
], $key['private_key'], 'RS256');
} catch (\Exception $exception) {
return throw new \Exception($exception->getMessage());
}
} else {
return throw new \Exception('Отсутствует обязательное поле client_email или private_key');
$batch = $this->service->createBatch();

foreach ($file as $line) {
$this->postBody->setType('URL_UPDATED');
$this->postBody->setUrl($line);
$batch->add($service->urlNotifications->publish($this->postBody));
}

return $batch->execute();
}
}

0 comments on commit bea2c11

Please sign in to comment.