I am trying to convert a command to php curl or Drupal::httpClient().
The original command
Curl -k -X PURGE -H "X-Acquia-Purge:[site-name]" --compressed -H "Host: example.com" https://bal-12345.prod.hosting.acquia.com/my-url
Here is the conversion I tried (but it is not working) This one returns 200 status code but it is not working.
$varnishUrl = "https://bal-12345.prod.hosting.acquia.com/my-url"
$headers = [
'X-Acquia-Purge' => "site-name",
'Host' => "example.com"
];
$options = ['headers' => $headers, 'body' => $varnishUrl];
try {
$client = \Drupal::httpClient();
$response = $client->request('PURGE', $domainUrl, $options);
$code = $response->getStatusCode();
if ($code == 200) {
return $code;
}
}
catch (RequestException $e) {
watchdog_exception('module_name', $e);
}
This is the second conversion to php and also not working.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $varnishUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PURGE');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'X-Acquia-Purge: site-name';
$headers[] = 'Host: example.com';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_exec($ch);
curl_close($ch);
I would appreciate any input.