How can a HEAD request be implemented in cURL to retrieve only the header information of a webpage in PHP?

To retrieve only the header information of a webpage using cURL in PHP, you can use a HEAD request. This type of request fetches only the headers of the response without downloading the actual content of the page. By setting the CURLOPT_NOBODY option to true in the cURL request, you can achieve this.

$url = 'https://example.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

if($response === false){
    echo 'Error: ' . curl_error($ch);
} else {
    echo $response;
}

curl_close($ch);