Skip to content

Instantly share code, notes, and snippets.

@geraldvillorente
Created November 22, 2021 23:18
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 geraldvillorente/4c7ec45ba262e3426192ecea2d09a423 to your computer and use it in GitHub Desktop.
Save geraldvillorente/4c7ec45ba262e3426192ecea2d09a423 to your computer and use it in GitHub Desktop.
Purge Cloudflare cache
{
"zone_id": "[ZONE_ID]",
"api_key": "[API_KEY]",
"email": "[EMAIL]"
}
<?php
// Run this script in Live environment only.
if ($_ENV['PANTHEON_ENVIRONMENT'] != 'live') {
die();
}
// Parse the secret file.
$config = json_decode(file_get_contents($_SERVER['HOME'] . '/files/private/cloudflare.json'), 1);
if ($config == false) {
die('Config not found. Aborting!');
}
// Clear cloudflare cache.
purge_cloudflare_cache($config);
function purge_cloudflare_cache($config) {
$request_url = 'https://api.cloudflare.com/client/v4/zones/' . $config['zone_id'] . '/purge_cache';
$payload = '{"hosts":["www.mydomain.com"]}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-Auth-Email: ' . $config['email'],
'X-Auth-Key: ' . $config['api_key'],
'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
print("\n==== Sending Request to Cloudflare ====\n");
$result = curl_exec($ch);
print('RESULT: ' . $result . "\n");
print("\n===== Request Complete! =====\n");
curl_close($ch);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment