For those who are not familiar let me explain what CloudProxy is. CloudProxy is the Web Application Firewall & Caching service from my employer, Sucuri.net. This service is setup at the DNS level and helps filtering all the HTTP requests that get to your website and speeding the performance a the same time.
Below is a simple graphic to explains how it works:
This is convenient when you are not making that many changes into your website. However if you are pushing changes everyday or building an webapp, then you need something easier.
Clearing the cache via PHP
I am currently building a web app with some friends and we faced the issue that after deploying each change, I needed to login to my account and clear the cache manually. This becomes tediously when you are committing changes several times a day 5 days a week. So I had to come up with a simple solution to get the job done.
That is why I turned to CloudProxy’s API, which offers me the ability to clear the cache by calling a simple string. You can see the basic code below:
* Simple Script in PHP to clear sucuri's cloudproxy cache via php
* Author: Salvador Aguilar
* Email: firstname.lastname@example.org
* Web: salrocks.com
$curl = curl_init();
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://waf.sucuri.net/api?v2',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
// this is the Sucuri CloudProxy API key for this website
k => 'your-cloudproxy-api-key-goes-here',
// this is the Sucuri CloudProxy API secret for this website
s => 'your-cloudproxy-api-secret-goes-here',
// this is the Sucuri CloudProxy API action for this website
a => 'clear_cache'
// Send the request & save response to $resp
$resp = curl_exec($curl);
echo '<pre>' . $resp . '</re>';
// Close request to clear up some resources
Once you have the file, you simply put it on your website root and you edit with the proper values from your Sucuri account and then simply run it via a web browser or command line and you cache will be cleared.
I added this script into my deployment script so I can update from my git repo and then clear the cache from Sucuri. Making things easy for us 🙂