Neon Web Design Poole

Screen Shot 2022 04 28 at 10.46.00

Automated Daily NitroPack Cache Purge

I have been trying to find a way to automate a daily purge of a nitropack cache on a server without wp-cli. I have out together some code based on the Nitropack API (https://docs.nitropack.io/) which is called daily via a cron job.

<?php

// Find these at https://nitropack.io/user/connect
$siteID = '[insert your site ID]';
$siteSecret = '[insert your site secret]';

// Create signature
$signature = hash_hmac('SHA512', "/cache/purge/$siteID||", $siteSecret);

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.getnitropack.com/cache/purge/".$siteID,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_HTTPHEADER => array(
    "X-Nitro-Signature: ".$signature
  )
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

?>

Place this in a file on the server (don’t forget to exclude it’s location from the Nitropack cache here: https://nitropack.io/user/settings), then setup a cron job to execute the code accordingly.

Scroll to Top