<?php  
/*
    Function get_remote_file()
    Variables:
        $file = the remote file wanted for download;
        $user_pass = the username and password in the form username:password
        $user_agent = a string to identify the script running;
        $cache_file = the location of the local cache of the requested file;
        $cache_time = the amount of time that should elapse before the file is re-downloaded. Defaults to 30 mins;
    Typical Call:
          $file = get_remote_file('http://del.icio.us/api/posts/recent?count=20', 'joe:blogs', 'Delicious', 'temp-file.txt', 1800);
*/
function get_remote_file($file$user_pass$user_agent$cache_file$cache_time='1800'){
    
// Set the username to something identifiable
    
$user_agent $user_agent."PHP/0.100 (" PHP_OS "/PHP-" phpversion() . ")";
    
    
// check to see how old the cache is 
    
if (file_exists($cache_file) && filemtime($cache_file) > time() - $cache_time){
        
// if cache is recent use it
        
$content file_get_contents($cache_file);
    } else {  
        
// Initialize cURL session
        
$curl curl_init($file);
        
// Set the additional headers to send
        
curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);  // return as a string
        
curl_setopt($curlCURLOPT_TIMEOUT30);  // set timeout
        
curl_setopt($curlCURLOPT_USERPWD$user_pass); // set user:pass
        
curl_setopt($curlCURLOPT_USERAGENT$user_agent);
        
// Perform the cURL session and record response
        
$output curl_exec($curl);
        
$outputInfo curl_getinfo($curl);
        
// close cURL connection
        
curl_close($curl);
        
// ensure file was successful
        
if($outputInfo['http_code'] == '200'){
            
// update cache
            
$content $output;
            
$save fopen($cache_file'w');
            
fwrite($save$content);
            
fclose($save);
        } else {
            
// if unsuccessful revert to cache
            
$content file_get_contents($cache_file);
        }
    }
    return 
$content;
}
?>