What are the benefits of using fsockopen in PHP for checking website availability compared to other methods?

When checking website availability in PHP, using fsockopen provides a low-level way to establish a socket connection to the website's server. This method allows for more control over the connection process and can be more efficient compared to other methods like using cURL or file_get_contents. Additionally, fsockopen can handle various protocols like HTTP, HTTPS, FTP, etc., making it versatile for different types of website checks.

function checkWebsiteAvailability($url, $port = 80) {
    $fp = @fsockopen($url, $port, $errno, $errstr, 5);
    if ($fp) {
        fclose($fp);
        return true;
    } else {
        return false;
    }
}

// Example usage
if (checkWebsiteAvailability('www.example.com')) {
    echo 'Website is available';
} else {
    echo 'Website is not available';
}