Purge of files not working in Drupal file

I want to purge cloudflare cache in my Drupal site, and to start, I tried the following through the console:

curl -X POST "https://api.cloudflare.com/client/v4/zones/zone_id/purge_cache" \
     -H "X-Auth-Email: [email protected]" \
     -H "X-Auth-Key: myapikey" \
     -H "Content-Type: application/json" \
     --data '{"purge_everything":true}'

Moving on to my code file, I created something like this through CURL:

$cURLConnection = curl_init("https://api.cloudflare.com/client/v4/zones/{$zone_id}/purge_cache");

curl_setopt($cURLConnection, CURLOPT_HTTPHEADER, [
  'accept: */*',
  'X-Auth-Email: [email protected]',
  "X-Auth-Key: {$myapikey}",
  'Content-Type: application/json'
]);

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

curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);

$apiResponse = curl_exec($cURLConnection);
curl_close($cURLConnection);

$jsonArrayResponse = json_decode($apiResponse);

return new ResourceResponse(json_encode($jsonArrayResponse));

I am getting the following error in the screen when making the request:

"{\u0022success\u0022:false,\u0022errors\u0022:[{\u0022code\u0022:7003,\u0022message\u0022:\u0022Could not route to \\\/zones\\\/purge_cache, perhaps your object identifier is invalid?\u0022},{\u0022code\u0022:7000,\u0022message\u0022:\u0022No route for that URI\u0022}],\u0022messages\u0022:[],\u0022result\u0022:null}"

Is there anything different that I need to add in curl request that should be different than the first script?

Note: I know of cloudflare_purge module but I opted to not use it because it throws a notice after the submission.

This must be replaced with your actual ZONE ID.
Hint: it’s a 32 character string which you can find here:


Probably you provided different information there, please double check you provide the correct ZONE ID.

3 Likes

Everything “was” ok, except I realized that I was pulling the wrong configuration form details.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.