Created
January 4, 2015 01:45
-
-
Save jacktasia/17cefd2c41a5b44d8460 to your computer and use it in GitHub Desktop.
imgix PHP purge example
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 | |
// find your api key on the bottom of page @ https://webapp.imgix.com | |
define('IMGIX_API_KEY', ''); | |
// pass the url you want to purge | |
function imgix_purge($url) { | |
$headers = array( | |
'Content-Type:application/json', | |
'Authorization: Basic '. base64_encode(IMGIX_API_KEY.':') | |
); | |
$payload = json_encode(array("url" => $url)); | |
$curl = curl_init('https://api.imgix.com/v2/image/purger'); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); | |
curl_setopt($curl, CURLOPT_TIMEOUT, 30); | |
curl_setopt($curl, CURLOPT_POST, 1); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
$response = curl_exec($curl); | |
curl_close($curl); | |
return $response; | |
} | |
// usage | |
$imgix_url = 'http://yourcompany.imgix.net/examples/mountain.jpg'; | |
echo imgix_purge($imgix_url); |
Since ImgIX was too lazy to provide updated code (and provided incorrect information), here is an updated version that is working for me as of February 2023:
<?php
// find your api key on the bottom of page @ https://dashboard.imgix.com/api-keys
define('IMGIX_API_KEY', 'YOUR_IMGIX_API_KEY_HERE');
// pass the url you want to purge
function imgix_purge($url) {
$headers = array(
'Authorization: Bearer ' . IMGIX_API_KEY,
'Content-Type:application/vnd.api+json'
);
$payload = json_encode(array(
"data" => array(
"attributes" => array(
"url" => $url
),
"type" => "purges"
)));
$curl = curl_init('https://api.imgix.com/api/v1/purge');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
// usage
$imgix_url = 'http://yourcompany.imgix.net/examples/mountain.jpg';
echo imgix_purge($imgix_url);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The imgix legacy purging API should be getting deprecated soon:
https://blog.imgix.com/2020/10/16/api-deprecation
I'd recommend following the migration guide for anyone still using this code:
https://docs.imgix.com/setup/legacy-api-migration-guide
To summarize:
https://api.imgix.com/v2/image/purger
Authorization: Bearer <api-key>
Content-Type: application/vnd.api+json
Note that for the API also now supports sub-image purging. You can read more about that here:
https://docs.imgix.com/apis/management#purge-sub-images
For any help, contact [email protected]