Created
November 22, 2021 23:18
-
-
Save iamstoick/4c7ec45ba262e3426192ecea2d09a423 to your computer and use it in GitHub Desktop.
Purge Cloudflare cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"zone_id": "[ZONE_ID]", | |
"api_key": "[API_KEY]", | |
"email": "[EMAIL]" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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