Is using cURL to retrieve the HTTP status the most reliable method to check the existence of a website URL in PHP?

Using cURL to retrieve the HTTP status of a website URL is a reliable method to check its existence in PHP. By sending a HEAD request using cURL, we can efficiently check the status code returned by the server. If the status code is in the 200 range, it indicates that the URL exists and is accessible.

$url = "https://www.example.com";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);

$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

if ($httpCode >= 200 && $httpCode < 300) {
    echo "URL exists and is accessible.";
} else {
    echo "URL does not exist or is not accessible.";
}