2013-01-20 67 views
2

使用我的wordpress functions.php文件來檢查顯示的每個圖像是否已啓動並正在運行或已關閉。我認爲我想要做的就是將這個功能代碼(下面)分解爲兩個。快速查找網站是關閉還是關閉?

函數1:檢查mirror1.com是否啓動(而不是檢查循環中的每個圖像)。根據mirror1.com的http狀態插入if/then語句。 (如果mirror1.com是向下,然後使用mirror2.com) - 傳遞到$ mirror_website

功能2:簡單地傳遞在$ mirror_website ..(前端具有<img src="<?php echo $mirror_website; ?>/image.png">

下面的代碼有效,但它檢查每個簡單的圖像,並減慢網站速度。

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 
    } 

回答

1

一個簡單的解決辦法可能是緩存的結果(例如,在APC或內存緩存)與TTL,所以你不需要工作了,如果該網站是向上或向下的突發狀況。

例如。下面是一個使用APC緩存2分鐘網站狀態結果的示例:

function amazons3acctreplaceto() { 
    $as3replaceto = FALSE; 
    if (function_exists('apc_fetch')) { 
    $as3replaceto = apc_fetch('as3replaceto'); 
    } 

    if ($as3replaceto === FALSE) { 
    $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 
    } 
    if (function_exists('apc_store')) { 
     apc_store('as3replaceto', $as3replaceto, 120); //Store status for 2 minutes 
    } 
    } 
+0

由於某種原因,上面的代碼不吐出$ as3replaceto; – 1xxooxx

+0

只有安裝並啓用了apc,它才能正常工作? – Wivlaro