PHP to Use Cloudflare API (Purge)

Here is the Cloudflare API guide on how to purge a single file or group of files:

curl -X DELETE "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/purge_cache" \
     -H "X-Auth-Email: user@example.com" \
     -H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
     -H "Content-Type: application/json" \
     --data '{"files":["http://www.example.com/css/styles.css"],"tags":["some-tag","another-tag"]}'

Not sure how I do that in PHP.

All feedback appreciated.

Cheers
Ryan

I have a PHP file on my CloudFlared Cached site with the following:

<?php 

   $delDir = isset($_GET['isDir'])  ? $_GET['isDir']  : NULL;
   $pWord  = isset($_GET['mypass']) ? $_GET['mypass'] : NULL;

   if($delDir && is_dir($delDir) && 'password-goes-here'===$pWord)
   { 
      $retval = 'byRef';
      #$ok = chown($delDir, 'www-data'); # ONLY superUser can change
      $ok = TRUE;
      if($ok)
      {
        echo 'system() ' .system('rm -rf ' . escapeshellarg($delDir), $retval);
        echo '<br />';
        echo '$reval : '.$retval;
        echo '<br />';
        
      }else{
        echo '<p># ONLY SuperUser can change</p>';
      }
   }

BEWARE:

Because the PHP “fm -rf” delete is unforgiving and instant.

Best to check before applying the delete otherwise you could easily delete your whole drive/site.

You have been warned :slight_smile:

I’m not sure how this would work. There is no guide they have saying this would work.

I want to just purge files from Cloudflare cache, not delete locally.

Why do do you want to delete CloudFlare files?

I usually login to CloudFlare:

  1. set “Overview->Status: Development Mode” which prevents caching for mySite for 3 hours
  2. use PkZip to modify or delete mySite files.
  3. set “Overview->Status: Development Mode” reset the option back from Development mode.

I have had no problems using this technique.

That would be an option, though most images on the site are create dynamically, with the expectation that a CDN or Cloudflare would cache them. If I was to go into Development mode the server wouldn’t stay online long.

I did find a solution, using a PHP class on github, a couple PHP edits, and local coding.

I am curious why you think your “server wouldn’t stay online long.”

Care to share your GitHub solution?

We take over 1.5M requests to dynamic images per day, and GD library struggles to keep up at times. We also take another 1M more requests to our embeds. And so on. We used to run a loadbalanced system, but shrank it down with the help of cacheing and CDNs.

Here is the Github:

The only flaw is that it has issues if you have more than one zone/site on Cloudflare under same account. The two fixes is just supply your ZoneID, instead of attempting to get zoneid each API call, or you can add this:

You don’t use the $identifier function provided, but instead just add this to your file that is including the class (after the included class of course):

$result = $api->get_zones();
foreach($result->result as $item){
    if($item->name=='yourdomain.com'){
      $identifier = $item->id;
      break;
    }
}

That’s the zone id, then can use rest of API is exampled.

I’ve added a page rule to Cloudflare, where if “/clear/” exists in URL, it bypasses. /clear/ launches a PHP script that takes the page url (without /clear/) and uses the API to clear that exact URL.

But, i’m now adding to other scripts, so any time we update a content page (or pages), it loops them through cloudflair purge API.

Cheers
Ryan

1 Like

That is a lot of images to process!

I should imagine you have tried virtually everything to improve performance.

No doubt you have tried ImageMagick and I am curious to know how it fared against PHP GD library?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.