What is the purpose of the function link_status in the PHP code provided?

The purpose of the function link_status in the PHP code provided is to check the status of a given URL by sending a HEAD request and returning the HTTP status code. If the function is not working as expected, it may be due to the use of file_get_contents which may not handle HEAD requests properly. To fix this issue, we can use cURL to send the HEAD request instead.

function link_status($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_exec($ch);
    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return $status;
}

// Example usage
$url = "https://www.example.com";
$status = link_status($url);
echo "Status code for $url: $status";