LoginSignup
17
17

More than 5 years have passed since last update.

PHP+LaravelでGoogle しごと検索の Indexing APIを呼び出す

Last updated at Posted at 2019-01-26

はじめに

Google しごと検索が日本でもリリースされました。求人情報をGoogleの検索結果に表示することができるようになります。

検索結果例
capture1.PNG

Googleによると、必要な手順は大まかに以下の3つになります

  1. 求人情報を構造化データ(JobPosting)でマークアップ
  2. Indexing APIを利用して更新/削除情報をGoogleに通知

1はGoogleの資料、構造化データタイプの定義を参照

2のIndexing APIをPHP+Laravelから利用してみます。なお、今日時点でIndexing APIの利用は無料です。

LaravelでGoogle しごと検索の Indexing APIを呼び出す

Google提供のクライアントライブラリをcomposerでプロジェクトに追加します。
https://github.com/googleapis/google-api-php-client

お約束。composer require

composer require google/apiclient:"^2.0"

composer.jsonにgoogle/apiclientが追加されました。

// (略)
  "require": {
    "php": "^7.1.3",
    "chillerlan/php-qrcode": "^2.0",
    "davejamesmiller/laravel-breadcrumbs": "5.x",
    "doctrine/dbal": "^2.5",
    "encore/laravel-admin": "1.6.*",
    "fideloper/proxy": "^4.0",
    "google/apiclient": "2.0",
// (略)

ライブラリインストールします

composer install

ライブラリが利用できるか確認するために、適当に呼び出してみました

use Google_Client;

// 略

$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$client->setDeveloperKey("YOUR_APP_KEY");
dd($client->getAccessToken());

=> null

よさそう。

続いて、
https://developers.google.com/search/apis/indexing-api/v3/prereqs?hl=ja
にあるPHPサンプルコードを貼り付けてみます
(jsonファイルはGoogle APIs コンソールから入手してconfigフォルダに置いた)

$client = new Google_Client();

$client->setAuthConfig('./config/ivory-totem-999999-1234567890.json');
$client->addScope('https://www.googleapis.com/auth/indexing');

$httpClient = $client->authorize();
$endpoint = 'https://indexing.googleapis.com/v3/urlNotifications:publish';

$content = '{
    "url": "https://job.e2info.co.jp/job/4611",
    "type": "URL_UPDATED"
}';

$response = $httpClient->post($endpoint, ['body' => $content]);

dd($response);

=> GuzzleHttp\Message\Response {#1000
  -reasonPhrase: "OK"
  -statusCode: 200
  -effectiveUrl: "https://indexing.googleapis.com/v3/urlNotifications:publish"
  -headers: array:11 [
    "content-type" => array:1 [
      0 => "application/json; charset=UTF-8"
    ]
    "vary" => array:3 [

なんと、Index送信成功しました:yum:

注意事項

  • Google曰く、サイトマップよりIndexing APIの使用をおすすめするが、サイトマップ送信もしましょうとのこと。つまり両方やったほうがいいです(文献

まとめ

ということで、あっという間に完成してしまいました。

終わり

17
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
17