-
-
Save Greg-Boggs/73796406278cd67334db08dc052931dd to your computer and use it in GitHub Desktop.
| <?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. | |
| $zoneId = "xxx"; | |
| $apiKey = "xxx"; | |
| $email = "xxx"; | |
| // optional URL for individual URL purge | |
| $file = 'https://example.com/style.css'; | |
| try { | |
| $head = []; | |
| $head[] = 'Content-Type: application/json'; | |
| $head[] = "X-Auth-Email: $email"; | |
| $head[] = "X-Auth-Key: $apiKey"; | |
| $head[] = 'cache-control: no-cache'; | |
| $url = "https://api.cloudflare.com/client/v4/zones/$zoneId/purge_cache"; | |
| // You can also purge files like: | |
| //$purge = ['files' => [$file]; | |
| $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); | |
| } |
Just add a loop and use a different zone-id to purge the caches you want to use.
Personally I am doing these kind of things based on information coming from a database. This way it is easy to log the results, and keep track when the purges were processed. If you are interested in such scripts you may visit : https://toolbox.tomdings.com/
Thank you for asking and enjoy your day!
This is the updated code in 2023.
$zoneId = "xxx";
$apiKey = "xxx";
$email = "xxx;
try {
$head = [];
$head[] = 'Content-Type: application/json';
$head[] = "X-Auth-Email: $email";
$head[] = "X-Auth-Key: $apiKey";
$head[] = 'cache-control: no-cache';
$url = "https://api.cloudflare.com/client/v4/zones/$zoneId/purge_cache";
// You can also purge files like:
$purge = ['files' => ['https://xxx.com/xxx]];
//$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);
}
I updated the code in the gist Tapan-thapa! Thanks for posting. Lemme know if I missed anything.
Please correct from file = 'https://example.com/style.css'; to $file = 'https://example.com/style.css';
Fixed
Thanks, this is useful code.
But for some reason, I get an error when I use a custom API key with cache purge permissions:
{"success":false,"errors":[{"code":10000,"message":"Authentication error"}]}
But using the Global API key works:
{ "result": { "id": "zoneidzoneid" }, "success": true, "errors": [], "messages": [] }
Or is the "success" message returned not for the cache clearing?
I believe the true is for the cache clearing being successful. Not sure about the API key permissions and Cloudflare. However, whenever I work with CF, their support staff and X engagement folks have always been pretty responsive.
great script, but can you purge cache to multiple domains (zones) ? I have 100+ domains and I wanna purge cache for all zones for current account.
Thanks!