這是我的代碼:腓捲曲:工作腳本,需要一點點添加
function get_remote_file_to_cache(){
$sites_array = array("http://www.php.net", "http://www.engadget.com", "http://www.google.se", "http://arstechnica.com", "http://wired.com");
$the_site= $sites_array[rand(0, 4)];
$curl = curl_init();
$fp = fopen("rr.txt", "w");
curl_setopt ($curl, CURLOPT_URL, $the_site);
curl_setopt($curl, CURLOPT_FILE, $fp);
curl_exec ($curl);
curl_close ($curl);
}
$cache_file = 'rr.txt';
$cache_life = '15'; //caching time, in seconds
$filemtime = @filemtime($cache_file);
if (!$filemtime or (time() - $filemtime >= $cache_life)){
ob_start();
echo file_get_contents($cache_file);
ob_get_flush();
echo " <br><br><h1>Writing to cache</h1>";
get_remote_file_to_cache();
}else{
echo "<h1>Reading from cache file:</h1><br> ";
ob_start();
echo file_get_contents($cache_file);
ob_get_flush();
}
一切正常,因爲它應該沒有問題或驚喜,你可以看到它的很簡單的代碼,但我是新來的CURL,並只想添加一個支票的代碼,但不知道如何:
有無論如何檢查從遠程站點獲取的文件不是404(未找到)頁面或類似的,但是一個狀態代碼200(成功)?
所以基本上,只寫入緩存文件如果填充是狀態碼200
謝謝!
也許這可以幫助: http://stackoverflow.com/questions/408405/easy-way-to-test-a-url-for-404-in-php – Vikk