How can PHP be used to verify the validity of a link and its content?

To verify the validity of a link and its content using PHP, you can make use of the cURL library. By sending a request to the URL and checking the response code, you can determine if the link is valid. Additionally, you can parse the content of the response to further verify its validity.

$url = "https://www.example.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if($httpCode == 200){
    echo "Link is valid.";
    // Further validation of content can be done here
} else {
    echo "Link is not valid.";
}

curl_close($ch);