Drupal Answers Asked by Alaa Haddad on November 29, 2020
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.
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;
}
Answered by Alaa Haddad on November 29, 2020
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP