What are some alternative methods or functions in PHP that can be used to check the reachability of a website, and how do they compare to using fsockopen()?

One alternative method in PHP to check the reachability of a website is using cURL functions. cURL is a library that allows you to make HTTP requests and handle responses in PHP. Compared to using fsockopen(), cURL is often considered more user-friendly and versatile for making HTTP requests.

function checkWebsiteReachability($url){
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    $response = curl_exec($ch);
    
    if($response === false){
        return "Website is not reachable";
    } else {
        return "Website is reachable";
    }
    
    curl_close($ch);
}

$url = "http://www.example.com";
echo checkWebsiteReachability($url);