おはこんちは
Google for jobsでお仕事の情報更新・削除するためにバッチを作らなければならなかったんだけどわかんねーよ
インデックス登録の一括リクエストを送信する
https://developers.google.com/search/apis/indexing-api/v3/using-api?hl=ja#batching
上記サイトで下の方に「詳しくは、バッチ リクエストの送信をご覧ください」のリンク先
https://developers.google.com/api-client-library/php/guide/batch?hl=ja
これじゃー分からんよ
Google先生でぐぐりまくりんぐ
最初の方にでてくる日本語サイトにはほぼ欲しい情報なっしんぐ。
どうやって行きついたかもわからないけどstackoverflowのサイト
https://stackoverflow.com/questions/53481191/how-to-send-batch-request-with-google-indexing-api
この通りにやればできたよ!ってかいてあるんだどできねーし
で、他にも調べてたらサービスライブラリにサービスがねーじゃん!!!
ってことで
GoogleAPIClinetをcomposerで入れない場合
GoogleAPIClinetはここから
https://github.com/googleapis/google-api-php-client/releases
何気にIndexingのサービスがないので以下からダウンロード
https://github.com/googleapis/google-api-php-client-services
バッチ処理で登録するプログラムソース
<?php
// Google Api Clientライブラリ読込
require_once('google-api-php-client/vendor/autoload.php');
// クライアントのインスタンス生成
$client = new Google_Client();
// 認証情報設定
$client->setAuthConfig('認証情報作成時のjsonファイル');
// スコープ設定
$client->addScope("https://www.googleapis.com/auth/indexing");
// バッチ使うんご
$client->setUseBatch(true);
// ここみそです。
$batch = new Google_Http_Batch($client,false,'https://indexing.googleapis.com');
// とりあえずお仕事のURLがこんな感じであるとする
$arr_url = array(
"https://example.jp/job?id=1",
"https://example.jp/job?id=2",
);
foreach ($arr_url as $url) {
$postBody = new Google_Service_Indexing_UrlNotification();
$postBody->setType('URL_UPDATED');//登録・更新
//$postBody->setType('URL_DELETE');//削除
// 削除をする場合、対象アドレスのHTTPステータスが404または410を返すようにする
$postBody->setUrl($url);
$service = new Google_Service_Indexing($client);
$request = $service->urlNotifications->publish($postBody);
$batch->add($request);
}
$results = $batch->execute();
print_r($results);
出力結果
Array
(
[response-1234567890] => Google_Service_Indexing_PublishUrlNotificationResponse Object
(
[urlNotificationMetadataType:protected] => Google_Service_Indexing_UrlNotificationMetadata
[urlNotificationMetadataDataType:protected] =>
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
[urlNotificationMetadata] => Google_Service_Indexing_UrlNotificationMetadata Object
(
[latestRemoveType:protected] => Google_Service_Indexing_UrlNotification
[latestRemoveDataType:protected] =>
[latestUpdateType:protected] => Google_Service_Indexing_UrlNotification
[latestUpdateDataType:protected] =>
[url] => https://example.jp/job?id=1
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
[latestUpdate] => Google_Service_Indexing_UrlNotification Object
(
[notifyTime] => 2019-02-08T09:56:51.949146599Z
[type] => URL_UPDATED
[url] => https://example.jp/job?id=1
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
)
)
)
[response-1234567891] => Google_Service_Indexing_PublishUrlNotificationResponse Object
(
[urlNotificationMetadataType:protected] => Google_Service_Indexing_UrlNotificationMetadata
[urlNotificationMetadataDataType:protected] =>
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
[urlNotificationMetadata] => Google_Service_Indexing_UrlNotificationMetadata Object
(
[latestRemoveType:protected] => Google_Service_Indexing_UrlNotification
[latestRemoveDataType:protected] =>
[latestUpdateType:protected] => Google_Service_Indexing_UrlNotification
[latestUpdateDataType:protected] =>
[url] => https://example.jp/job?id=2
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
[latestUpdate] => Google_Service_Indexing_UrlNotification Object
(
[notifyTime] => 2019-02-08T09:56:51.947522868Z
[type] => URL_UPDATED
[url] => https://example.jp/job?id=2
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
)
)
)
)
最後に、バッチを4回投げたとしてもIndexing APIの実行回数は、4回ではなく・・・、上記の例だと1バッチで100回の登録更新をしているので、400回リクエストを投げているのと同じ分APIが消費してしまいます。
なので、1日の割り当て数(初期に200回)などを更新などしなくてはいけないようです。
実際にそんなに登録・更新・削除の件数があるわけではないので最初だけ大量に実行する必要がある。