How can the HTTP status code be retrieved using PHP to determine if a link is reachable?
To determine if a link is reachable, you can use PHP to make a request to the URL and retrieve the HTTP status code. This can be done using the `get_headers()` function in PHP, which returns an array of headers sent by the server in response to an HTTP request. You can then extract the HTTP status code from the headers array to determine if the link is reachable.
function isLinkReachable($url) {
$headers = get_headers($url);
if ($headers) {
$statusCode = substr($headers[0], 9, 3);
return (int)$statusCode < 400;
} else {
return false;
}
}
// Example usage
$url = 'https://www.example.com';
if (isLinkReachable($url)) {
echo 'Link is reachable';
} else {
echo 'Link is not reachable';
}