-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathcloudflare_cache.php
More file actions
36 lines (31 loc) · 1.18 KB
/
cloudflare_cache.php
File metadata and controls
36 lines (31 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
// TODO: this header should not be required...
header('Content-Type: text/plain; charset=UTF-8');
// Only purge Cloudflare cache when the live environment's cache is cleared.
if ($_ENV['PANTHEON_ENVIRONMENT'] != 'live') {
die();
}
// Retrieve Cloudflare config data
$config_file = $_SERVER['HOME'] . '/files/private/cloudflare_cache.json';
$config = json_decode(file_get_contents($config_file), 1);
if ($config == FALSE) {
die('files/private/cloudflare_cache.json not found. Aborting!');
}
purge_cache($config);
function purge_cache($config) {
$payload = json_encode(array('purge_everything' => TRUE));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.cloudflare.com/client/v4/zones/' . $config['zone_id'] . '/purge_cache');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
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");
print("\n===== Request Complete! =====\n");
curl_close($ch);
}