Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
imgix PHP purge example
<?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);
@jonathan-bird

This comment has been minimized.

Copy link

@jonathan-bird jonathan-bird commented Oct 31, 2016

Great work. And for anyone reading this, here's the Guzzle 5.x implementation:

private function purgeImgixCache($imageUrl)
{
    $client = new Client();

    try {
        $request = $client->post('https://api.imgix.com/v2/image/purger',
            [
                'Content-Type'  => 'application/json',
                'Authorization' => 'Basic ' . base64_encode('THIS_IS_API_KEY_STRING' . ':')
            ],
            [
                'url' => $imageUrl
            ]
        );

        $response = $request->send();
    } catch (\Guzzle\Http\Exception\ClientErrorResponseException $exception) {
        $responseBody = $exception->getResponse()->getBody(true);

        return false;
    }

    return $response;
}
@sambshapiro

This comment has been minimized.

Copy link

@sambshapiro sambshapiro commented Jan 19, 2018

This was my final solution for Guzzle 6.

use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Client;

    private function purgeImgixUrl($url) {
        $client = new Client();
        try {
            $response = $client->post('https://api.imgix.com/v2/image/purger', [
                'headers' => [
                    'Content-Type'  => 'application/json',
                    'Authorization' => 'Basic ' . base64_encode($this->ImgixApiToken . ':')
                ],
                \GuzzleHttp\RequestOptions::JSON => ['url' => $url]
            ]);
        }
        catch (\Guzzle\Http\Exception\ClientErrorResponseException $exception) {
            $responseBody = $exception->getResponse()->getBody(true);
            echo $responseBody;
            return false;
        }
    }
@bhuvidya

This comment has been minimized.

Copy link

@bhuvidya bhuvidya commented Mar 22, 2018

thanks so much @sambshapiro that worked great for me - and saved me time!

@heyitsbryanm

This comment has been minimized.

Copy link

@heyitsbryanm heyitsbryanm commented Jun 29, 2021

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:

  • The endpoint will be changing to https://api.imgix.com/v2/image/purger
  • The authentication will be changing from "Basic" to "Bearer": Authorization: Bearer <api-key>
    • You will need to generate new API keys from the dashboard as well
  • You must specify Content-Type: application/vnd.api+json
  • The body of the request must be sent in the following format:
{
    "data": {
        "attributes": {
            "url": "https://assets.imgix.net/examples/bridge.jpg"
        },
        "type": "purges"
    }
}

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 support@imgix.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment