LoginSignup
5
1

More than 3 years have passed since last update.

PHP(Laravel)からAkamaiのFastPurgeAPI v3を叩く

Posted at

AkamaiのAPIのv1~v2のEOLが、2019年6月30日とのことで、これまでv2を利用していましたが、v3以降に移行する必要が出てきました。

v2のときは、APIの認証がcurlのヘッダにBasic認証情報を入れるだけで簡単に叩けるようになっていたのですが、v3(OPEN API)を使うためには、管理画面からAPIクライアントを登録して、認証情報を発行しなければいけません。

今回の記事では、Akamai Fast Purge API v3 をPHP(Laravel)から叩くようにするところまで記述したいと思います。

事前準備

まずはcredential情報をAkamaiの管理画面から発行しましょう。
https://developer.akamai.com/api/getting-started
こちらの公式ドキュメントの指示にしたがって、作業を行えば大丈夫です。

発行したcredential情報をコピーして、下記のような.edgercというファイルを作ったら、とりあえず準備は完了です。

 .edgerc
[default]
client_secret = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=
host = xxxxx.luna.akamaiapis.net/
access_token = xxxxx
client_token = xxxxx

composerで必要なライブラリをインストール

$ composer require akamai-open/edgegrid-client

public/.edgercを配置する

https://github.com/akamai/AkamaiOPEN-edgegrid-php-client
こちらによると、.edgercはWebサーバのホームディレクトリか、カレントワーキングディレクトリに置いてねとのことなので、
Laravelのプロジェクトだとpublic/に置くのが良いかと思います。

PHP側の処理

use \Akamai\Open\EdgeGrid\Client as AkamaiApiClient;


    // キャッシュクリアしたいURLを配列で列挙
    $objects = [
        'http://example.com/hoge.png',
        'https://example.com/hogehoge.jpg'
    ];

    // APIクライアント呼び出し, createFromEdgeRcFileの引数に何も与えなければ.edgercの[default]の認証情報がセットされます。
    $client = AkamaiApiClient::createFromEdgeRcFile();

    // Fast Purge v3 API 呼び出し、キャッシュクリア
    $response = $client->request('POST', '/ccu/v3/delete/url', [
        'body' => json_encode(['objects' => $objects,]),
        'headers' => ['Content-Type' => 'application/json',],
        'proxy' =>  'http://example-proxy.jp:8080', // プロキシの設定が必要な場合のみこちらを記述
    ]);

    echo $response->getStatusCode();

objectにURLを詰め込みます。
headersには、'Content-Type' => 'application/json'を指定
proxyは設定の必要がなければ削除してしまって構いません。
getStatusCode()201が帰ってきていたら成功です。
以上になります。

参考にしたページなど

これ以外にも実装方法は複数ありますので、
別の実装を検討される方は公式のサンプルやドキュメントを参照されるのが良いかと思われます。
https://github.com/akamai/api-kickstart/blob/master/examples/php/ccu_v3.php
https://developer.akamai.com/api/core_features/fast_purge/v3.html#cd1e7257

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