Last active
November 21, 2024 10:47
-
-
Save mskian/149d62c584cbe7e886a9fdd6d9652261 to your computer and use it in GitHub Desktop.
PHP API to Purge Cloudways Varnish Cache for Specific Server and App.
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 | |
## Replace the default details | |
define('CLOUDWAYS_API_URL', 'https://api.cloudways.com/api/v1'); | |
define('EMAIL', 'CLOUDWAYS ACCOUNT EMAIL'); | |
define('API_KEY', 'CLOUDWAYS ACCOUNT API KEY'); | |
define('SERVER_ID', 'CLOUDWAYS SERVER ID'); | |
define('APP_ID', 'CLOUDWAYS APPLICATION ID'); | |
header('X-Frame-Options: DENY'); | |
header('X-XSS-Protection: 1; mode=block'); | |
header('X-Content-Type-Options: nosniff'); | |
header('Strict-Transport-Security: max-age=63072000'); | |
header('Content-Type: application/json'); | |
header('X-Robots-Tag: noindex, nofollow', true); | |
/** | |
* Function to make a cURL request. | |
*/ | |
function makeCurlRequest(string $url, string $method = 'GET', array $headers = [], ?string $data = null): array | |
{ | |
$ch = curl_init(); | |
$options = [ | |
CURLOPT_URL => $url, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_HTTPHEADER => $headers, | |
CURLOPT_CUSTOMREQUEST => $method, | |
CURLOPT_TIMEOUT => 30, | |
CURLOPT_SSL_VERIFYPEER => true, | |
]; | |
if ($data) { | |
$options[CURLOPT_POSTFIELDS] = $data; | |
} | |
curl_setopt_array($ch, $options); | |
$response = curl_exec($ch); | |
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
if (curl_errno($ch)) { | |
throw new RuntimeException('cURL Error: ' . curl_error($ch)); | |
} | |
curl_close($ch); | |
return [ | |
'httpCode' => $httpCode, | |
'response' => $response, | |
]; | |
} | |
/** | |
* Function to obtain a Bearer token. | |
*/ | |
function getBearerToken(): string | |
{ | |
$url = CLOUDWAYS_API_URL . '/oauth/access_token'; | |
$data = json_encode([ | |
'email' => EMAIL, | |
'api_key' => API_KEY, | |
]); | |
$headers = [ | |
'Content-Type: application/json', | |
]; | |
$result = makeCurlRequest($url, 'POST', $headers, $data); | |
$response = json_decode($result['response'], true); | |
if ($result['httpCode'] !== 200 || empty($response['access_token'])) { | |
throw new RuntimeException(json_encode([ | |
'error' => 'Failed to obtain Bearer token', | |
'details' => $response, | |
])); | |
} | |
return $response['access_token']; | |
} | |
/** | |
* Function to purge cache. | |
*/ | |
function purgeCache(string $token): void | |
{ | |
$url = CLOUDWAYS_API_URL . '/app/cache/purge'; | |
$data = http_build_query([ | |
'server_id' => SERVER_ID, | |
'app_id' => APP_ID, | |
]); | |
$headers = [ | |
'Authorization: Bearer ' . $token, | |
'Content-Type: application/x-www-form-urlencoded', | |
'Accept: application/json', | |
]; | |
$result = makeCurlRequest($url, 'POST', $headers, $data); | |
$response = json_decode($result['response'], true); | |
if ($result['httpCode'] === 200 && isset($response['status']) && $response['status'] === true) { | |
echo json_encode([ | |
'success' => true, | |
'message' => 'Cache purged successfully', | |
'operation_id' => $response['operation_id'], | |
]); | |
} else { | |
throw new RuntimeException(json_encode([ | |
'success' => false, | |
'error' => 'Failed to purge cache', | |
'http_code' => $result['httpCode'], | |
'details' => $response, | |
])); | |
} | |
} | |
try { | |
$token = getBearerToken(); | |
purgeCache($token); | |
} catch (Throwable $e) { | |
echo json_encode([ | |
'success' => false, | |
'error' => $e->getMessage(), | |
]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment