What are some common methods for verifying the validity of hyperlinks in PHP and handling different response statuses?

When verifying the validity of hyperlinks in PHP, one common method is to use the cURL extension to make HTTP requests and check the response status code. By checking the response status code, you can determine if the link is valid (e.g., status code 200) or if there is an issue (e.g., status code 404). You can handle different response statuses by using conditional statements to take appropriate actions based on the status code.

function verifyLink($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_exec($ch);
    
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    
    if ($httpCode == 200) {
        echo "Link is valid";
    } else {
        echo "Link is not valid (HTTP code: $httpCode)";
    }
    
    curl_close($ch);
}

// Example usage
verifyLink("https://www.example.com");