Are there any alternative methods or functions in PHP that can be used to check website reachability besides fsockopen()?

The fsockopen() function in PHP can be used to check website reachability by establishing a socket connection to the specified host and port. However, an alternative method to check website reachability is by using the cURL extension in PHP. cURL allows you to make HTTP requests to a URL and check for a successful response.

function checkWebsiteReachability($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_exec($ch);
    
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    
    curl_close($ch);
    
    if ($httpCode == 200) {
        return true;
    } else {
        return false;
    }
}

$url = "http://www.example.com";
if (checkWebsiteReachability($url)) {
    echo "Website is reachable.";
} else {
    echo "Website is not reachable.";
}