I've seen a couple of related issues, but unfortunately, none of the recommended solutions had solved my problem.
I'm trying to send in a blank Accept header using PHP curl:
PHP Version: 7.3 Disto: Debian
/**
* @param string $method
* @param string $url
* @param string $body
* @param array $headers
*
* @return Response
*/
public function execute(string $method, string $url, string $body = '', array $headers = []): Response
{
$curlHeaders = [];
$isAcceptHeaderPresent = false;
$isExpectHeaderPresent = false;
$curl = \curl_init();
$response = new Response();
foreach ($headers as $name => $value) {
$curlHeaders[] = $value === '' ? "{$name};" : "{$name}: $value";
$name = \strtolower($name);
if ($name === 'accept') {
$isAcceptHeaderPresent = true;
}
if ($name === 'expect') {
$isAcceptHeaderPresent = true;
}
}
if ($isAcceptHeaderPresent === false) {
$curlHeaders[] = 'Accept: ';
}
if ($isExpectHeaderPresent === false) {
$curlHeaders[] = 'Expect: ';
}
$options = [
CURLOPT_URL => \str_replace(' ', '%20', $url),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_MAXREDIRS => 10,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POSTFIELDS => $body,
CURLOPT_HEADERFUNCTION => [$response, 'headerWrite'],
CURLOPT_WRITEFUNCTION => [$response, 'bodyWrite'],
CURLOPT_FILE => $response->getBodyAsResouce(),
CURLOPT_HTTPHEADER => $curlHeaders,
];
switch ($method) {
case 'GET':
case 'POST':
case 'PATCH':
case 'PUT':
case 'HEAD':
case 'DELETE':
case 'CONNECT':
case 'OPTIONS':
case 'TRACE':
$options[CURLOPT_CUSTOMREQUEST] = $method;
break;
default:
throw new \Exception('Unsupported HTTP method received: ' . $method);
}
\curl_setopt_array($curl, $options);
\curl_exec($curl);
$response->setStatusCode(\curl_getinfo($curl)['http_code']);
\curl_close($curl);
return $response;
}
If Accept:
is provided, then no Accept header will be sent in what so ever.
Whereas Accept;
make it so, that the blank value will be sent in along with */*
value as well: "accept":"*/*,
Is there a way to send in just the Accept:
header as is?
I wonder if this mechanism has been changed in the last couple of PHP and curl extension releases, given that none of the suggestions are working as intended.