0

I am creating a custom module to clear caches on cloudflare for one url. The original curl command:

curl -X POST "https://api.cloudflare.com/client/v4/zones/123456789/purge_cache" \
     -H "Authorization: Bearer 123456789" \
     -H "Content-Type: application/json" \
     --data '{"files": ["https://example.com", {"url": "https://example.com/my-url"}]}'

Drupal::httpClient

try {
  $client = \Drupal::httpClient();
  $url = "https://api.cloudflare.com/client/v4/zones/123456789/purge_cache";
  $options = [
    'headers' => [
      'Authorization' => 'Bearer 123456789',
    ],
    'json' => [
      'files' => 'https://example.com',
      'url' => 'https://example.com/my-url'
    ],
  ];
  $response = $client->request('POST', $url, $options);
  $code = $response->getStatusCode();
  if ($code == 200) {
    return $code;
  }
}
catch (RequestException $e) {
  watchdog_exception('module_name', $e);
}

php version:

$data = Json::encode(['files' => 'https://example.com', 'url' => 'https://example.com/my-url' ] );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.cloudflare.com/client/v4/zones/123456789/purge_cache');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$headers = array();
$headers[] = 'Authorization: Bearer 123456789';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_exec($ch);
curl_close($ch);

I got one working by clearing all caches on clouldflare. Original:

curl -X POST "https://api.cloudflare.com/client/v4/zones/123456789/purge_cache" \
     -H "Authorization: Bearer 123456789" \
     -H "Content-Type: application/json" \
     --data '{"purge_everything":true}'

The working Drupal Version:

    try {
      $client = \Drupal::httpClient();
      $options = [
        'json' => [
          'purge_everything' => TRUE,
        ],
        'headers' => [
          'Authorization' => 'Bearer ' . $authorization,
        ],
      ];
      $response = $client->request($method, $url, $options);
      $code = $response->getStatusCode();
      if ($code == 200) {
        return $code;
      }
    }
    catch (RequestException $e) {
      watchdog_exception('cloudflare_purge', $e);
    }

The drupal version return 200, but it is not doing anything and the php version I could not get it work. I would appreciate any input.

1
  • 3
    Cloudflare has a PHP SDK that supports zone cache purging. You'd be better off including that in your module's Composer dependencies instead of having to build your own CF API client.
    – Shawn Conn
    Commented Sep 6, 2020 at 0:02

1 Answer 1

0

https://api.cloudflare.com/#zone-purge-files-by-url I found the solution, so this command:

curl -X POST "https://api.cloudflare.com/client/v4/zones/abcdefghijklabcdefghijkl/purge_cache" \
     -H "Authorization: Bearer abcdefghijklabcdefghijkl" \
     -H "Content-Type: application/json" \
     --data '{"files": ["https://example.com", {"url": "https://example.com/url"}]}'

It will be in Drupal like:

      $client = \Drupal::httpClient();
      $options = [
        'headers' => [
          'Authorization' => 'Bearer abcdefghijklabcdefghijkl',
        ],
        'json' => [
          'files' => ['https://example.com/url']
        ],
      ];
      $response = $client->request('POST', $url, $options);
      $data = $response->getBody()->getContents();
      $data = json_decode($data, TRUE);
      if ($data['success']  == TRUE){
        return TRUE;
      }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.