Are there alternative methods in PHP to check if a webpage is reachable without using file()?

When checking if a webpage is reachable in PHP, the file() function can be used to retrieve the contents of a webpage. However, if the file() function is disabled or restricted on the server, an alternative method is to use cURL to make an HTTP request to the webpage and check the response code. This allows for checking the webpage's availability without relying on the file() function.

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

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

$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ($httpCode == 200) {
    echo "Webpage is reachable";
} else {
    echo "Webpage is not reachable";
}

curl_close($ch);