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";
Keywords
Related Questions
- Are there best practices for handling character encoding and file paths in PHP scripts, especially when transitioning from ISO-8859-1 to UTF-8?
- How can the length of a URL string in an email affect its display and functionality, especially when including parameters like email addresses and activation codes?
- How can PHP developers ensure efficient and optimized code when using string manipulation functions like explode?