Are there alternative methods to fsockopen for checking URL availability in PHP?

The fsockopen function in PHP can be used to check the availability of a URL by establishing a socket connection. However, an alternative method to achieve the same result is by using cURL, which is a library that allows you to make HTTP requests in PHP. By using cURL, you can easily check the availability of a URL without needing to manually establish a socket connection.

function checkUrlAvailability($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_exec($ch);
    $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    if ($statusCode == 200) {
        return true;
    } else {
        return false;
    }
}

$url = "https://www.example.com";
if (checkUrlAvailability($url)) {
    echo "URL is available.";
} else {
    echo "URL is not available.";
}