0

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!

Ryan
  • 9,821
  • 22
  • 66
  • 101
  • 1
    Maybe this can help: http://stackoverflow.com/questions/408405/easy-way-to-test-a-url-for-404-in-php – Vikk Nov 01 '11 at 02:23

2 Answers2

1

Try this after curl_exec

$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); 
samura
  • 4,375
  • 1
  • 19
  • 26
1

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.

Thai
  • 10,746
  • 2
  • 45
  • 57
  • "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." Got some code? I am new to curl :( . " try to load the page to page too" - what do you mean here? – Ryan Nov 01 '11 at 02:35
  • @Ryan, what I mean is that, you will have to write to another file, like rr_tmp.txt, then later copy it to rr.txt, so that when it fails, rr.txt will not get changed. If you use this approach, when you write to a temporary file, the cache file (rr.txt) is remains untouched, and it's possible that while the cURL operation is running, another user may come to your page and also see that the cache file is outdated, so both user makes the web server reload the cache. – Thai Nov 01 '11 at 02:46