Cloudflareでキャッシュをパージするには管理画面にログインする必要があります。
CloudflareのAPI経由でもキャッシュをパージできるので、Wordpressのダッシュボードからパージできるプラグインも存在しています。
公式の例だとcURLの例しかなく、PHPから呼び出したい時にすぐに使えません。
では、キャッシュのパージAPIをPHPから使うにはどうしたらよいでしょうか?
APIを使うために必要な情報
APIを利用するために必要な情報は以下の3つです。
Cloudflareの管理画面にログインすれば確認できます。
- zone id
- Cloudflareの登録メールアドレス
- API key
APIの公式サイト
APIとcURLの例が載っているのは以下のサイトです。
Cloudflare API | overview
Interact with Cloudflare's products and services via the Cloudflare API
全てのキャッシュをパージするcURLの例
curl -X POST "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/purge_cache" \
-H "X-Auth-Email: user@example.com" \
-H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
-H "Content-Type: application/json" \
--data '{"purge_everything":true}'
cURLをPHPに変換できるサイトを使う
Cloudflare公式にあるcURLの例をPHPに変換するのはcURLの知識が必要になりますが、実は便利なサイトを使うと簡単に変換でできます。
curl-to-PHP: Convert Curl commands to PHP code
全てのキャッシュをパージするAPIの変換結果
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/purge_cache');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"purge_everything\":true}");
$headers = array();
$headers[] = 'X-Auth-Email: user@example.com';
$headers[] = 'X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
公開されいてるPHPスクリプトを使う
他の人が公開しているスクリプトを流用することでcURLから変換せずにPHPに組み込めます。
しかし、認証方法など古い場合があるので注意が必要です。検索で出てくるgithubのスクリプトは認証エラーになりました。
ヘッダーのX-Auth-KeyがAuthentificationになっていてエラーになっていました。原因がわかるまで結構時間がかかりました。
エラーで動かない時は公式のcURLを変換するなどしてデバッグした方が良いです。

PHP code to Purge Cloudflare Cache
PHP code to Purge Cloudflare Cache. GitHub Gist: instantly share code, notes, and snippets.
<?php
// Replace EMAIL/API_KEY/ZONE_ID with your details.
// Zone ID is on the dashboard for the domain in the bottom right.
// Api keys are generated from the account settings. You must give cache purge permissions
// Place this script on your webserver and point a Github Webhook at it, and you'll clear
// the Cloudflare cache every time you do a push to GH.
try {
$head = [];
$head[] = 'Content-Type: application/json';
$head[] = 'X-Auth-Email: EMAIL';
$head[] = 'X-Auth-Key: Bearer API_KEY';
$head[] = 'cache-control: no-cache';
$url = 'https://api.cloudflare.com/client/v4/zones/ZONE_ID/purge_cache';
// You can also purge files like:
// $purge = ['files' => ['example.com/styles.css']]
$purge = ['purge_everything' => true];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_HTTPHEADER, $head);
curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($purge));
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
}
catch(Exception $e) {
print($e);
}
まとめ
Cloudflareはcache everythingのページルールを使えばすべてキャッシュしてくれます。
その反面、キャッシュのパージは変更のあったページ単位で行わないと、変更のないページまでパージされてしまいます。
APIを使えば、必要最低限のパージ範囲で済むので便利だと思います。