Cloudflare Clear Cache For One URL, Convert Curl Command to Drupal::httpClient() Or php I am creating a custom module to clear caches on cloudflare for one url.
The original curl command:
curl -X POST &quot;https://api.cloudflare.com/client/v4/zones/123456789/purge_cache&quot; \
     -H &quot;Authorization: Bearer 123456789&quot; \
     -H &quot;Content-Type: application/json&quot; \
     --data '{&quot;files&quot;: [&quot;https://example.com&quot;, {&quot;url&quot;: &quot;https://example.com/my-url&quot;}]}'

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

php version:
$data = Json::encode(['files' =&gt; 'https://example.com', 'url' =&gt; '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 &quot;https://api.cloudflare.com/client/v4/zones/123456789/purge_cache&quot; \
     -H &quot;Authorization: Bearer 123456789&quot; \
     -H &quot;Content-Type: application/json&quot; \
     --data '{&quot;purge_everything&quot;:true}'

The working Drupal Version:
    try {
      $client = \Drupal::httpClient();
      $options = [
        'json' =&gt; [
          'purge_everything' =&gt; TRUE,
        ],
        'headers' =&gt; [
          'Authorization' =&gt; 'Bearer ' . $authorization,
        ],
      ];
      $response = $client-&gt;request($method, $url, $options);
      $code = $response-&gt;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.
https://api.cloudflare.com/#zone-purge-files-by-url
I found the solution, so this command:
curl -X POST &quot;https://api.cloudflare.com/client/v4/zones/abcdefghijklabcdefghijkl/purge_cache&quot; \
     -H &quot;Authorization: Bearer abcdefghijklabcdefghijkl&quot; \
     -H &quot;Content-Type: application/json&quot; \
     --data '{&quot;files&quot;: [&quot;https://example.com&quot;, {&quot;url&quot;: &quot;https://example.com/url&quot;}]}'

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