Search code examples
apicloudflarepurge

How to make php api call to CloudFlare to Purge individual files by URL from cache


this code worked good to Purge All files from cache .

Example:

$authKey = "MyKEY";
$authEmail = "myEMAIL";

$zoneId = "MYZONEID";
$endpoint = "purge_cache";

$data = [
    "purge_everything" => true
];

$url = "https://api.cloudflare.com/client/v4/zones/{$zoneId}/{$endpoint}";
$opts = ['http' => [
    'method' => 'DELETE',
    'header' => [
        "Content-Type: application/json",
        "X-Auth-Key: {$authKey}",
        "X-Auth-Email: {$authEmail}",
    ],
    'content' => json_encode($data),
]];
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);

But I need Purge individual files by URL how can I do that via PHP ?

On api page I found this link: https://api.cloudflare.com/#zone-purge-individual-files-by-url

but I don't now how to make it via php ?


Solution

  • You need to pass an array with files - like explained on the API Docs

    In your example that would be something like

    $data = '{"files":[
      "http://www.example.com/css/styles1.css",
      "http://www.example.com/css/styles2.css",
      "http://www.example.com/css/styles3.css"
    ]}';