Php curl:工作脚本,需要一点补充

This is my code:

    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();

}

Everything works as it should and no problems or surprises, and as you can see its pretty simple code but I am new to CURL and would just like to add one check to the code, but dont know how:

Is there anyway to check that the file fetched from the remote site is not a 404 (not found) page or such but is a status code 200 (successful) ?

So basically, only write to cache file if the fill is status code 200.

Thanks!

To get the status code from a cURL handle, use curl_getinfo after curl_exec:

$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

But the cached file will be overwritten when

$fp = fopen("rr.txt", "w");

is called, regardless of the HTTP code, this means that to update the cache only when status is 200, you need to read the contents into memory, or write to a temporary file. Then finally write to the real file if the status is 200.

It is also a good idea to

touch('rr.txt');

before executing cURL, so that the next request that may come before the current operation finish will not also try to load the page to page too.

Try this after curl_exec

$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);