What are some best practices for handling cases where a website in an iframe is not reachable in PHP?

When a website in an iframe is not reachable in PHP, one solution is to use the cURL library to check the availability of the website before attempting to load it in the iframe. By making a request to the website URL and checking the response code, we can determine if the website is reachable or not.

$url = 'https://example.com';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);

$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if($httpCode == 200) {
    // Website is reachable, load it in the iframe
    echo '<iframe src="' . $url . '"></iframe>';
} else {
    // Website is not reachable, display an error message
    echo 'Website is not reachable.';
}

curl_close($ch);