Skip to content

Instantly share code, notes, and snippets.

@Sinepel
Forked from Greg-Boggs/purge_cf.php
Created April 20, 2021 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sinepel/9f436de5e7c4559638315216a3646441 to your computer and use it in GitHub Desktop.
Save Sinepel/9f436de5e7c4559638315216a3646441 to your computer and use it in GitHub Desktop.
PHP code to Purge Cloudflare Cache
<?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[] = 'Authorization: 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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment