PHP code example of elliotjreed / cloudflare-cache-purge

1. Go to this page and download the library: Download elliotjreed/cloudflare-cache-purge library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

elliotjreed / cloudflare-cache-purge example snippets




use ElliotJReed\Exception\Cloudflare;
use ElliotJReed\Zones;
use GuzzleHttp\Client;

try {
    $zoneResponse = (new Zones(new Client(), 'SECRET CLOUDFLARE API TOKEN'))
        ->get();

    foreach ($zoneResponse->getResults() as $result) {
        echo 'Zone ID: ' . $result->getId() . \PHP_EOL;
        echo 'Zone name: ' . $result->getName() . \PHP_EOL . \PHP_EOL;
    }
} catch (Cloudflare $exception) {
    echo $exception->getMessage() . \PHP_EOL;
    echo $exception->getPrevious()->getMessage() . \PHP_EOL;
}




use ElliotJReed\Cache;
use ElliotJReed\Exception\Cloudflare;
use GuzzleHttp\Client;

$urls = [
    'https://www.example.com.com/image1.png',
    'https://www.example.com.com/image2.png'
];

try {
    $cacheResponse = (new Cache(new Client(), 'SECRET CLOUDFLARE API TOKEN'))
        ->purgeFiles('zone-id-from-api-or-dashboard', $urls);

    foreach ($cacheResponse->getResults() as $result) {
        echo 'Cache Purge Response ID: ' . $result->getId() . \PHP_EOL . \PHP_EOL;
    }
} catch (Cloudflare $exception) {
    echo $exception->getMessage() . \PHP_EOL;
    echo $exception->getPrevious()->getMessage() . \PHP_EOL;
}

bash
php composer.phar install