Using my wordpress functions.php file to check if every image displayed is up and running or down. I think what I want to do is to break this functions code (below) into two.
function 1: check if mirror1.com is up (instead of checking every image in loop). insert if/then statement depending on http status of mirror1.com. (if mirror1.com is down, then use mirror2.com) — pass that into $mirror_website
function 2: simply pass in $mirror_website.. (the front end has <img src="<?php echo $mirror_website; ?>/image.png">
)
The code below works, but it’s checking EVERY simple image and slows down the site.
function amazons3acctreplaceto() {
$url = 'http://www.mirror1.com';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if (200==$retcode) {
$as3replaceto = "www.mirror1.com"; // All's well
} else {
$as3replaceto = "www.mirror2.com"; // not so much
}
A simple solution might be to cache the result (eg. in the APC or memcache) with a TTL so you don’t need to work out if the site is up or down for every eventuality.
eg. Here’s an example that might work using APC to cache the result of the site status for 2 minutes: