Reputation: 1924
I am using cURL to get an XML feed and save it into an XML file, like so
if (file_exists($filename) && (filemtime($filename) > time() - 30)) {
// load cached XML from file
$output = simplexml_load_file($filename);
return $output;
} else {
// Initiate the curl session
$ch = curl_init();
// Set the URL
curl_setopt($ch, CURLOPT_URL, $url);
// Removes the headers from the output
curl_setopt($ch, CURLOPT_HEADER, 0);
// Callback to get custom headers
curl_setopt($ch, CURLOPT_HEADERFUNCTION, array(&$this,'readHeader'));
// Return the output instead of displaying it directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Execute the curl session
$output = curl_exec($ch);
// Return headers
$headers = curl_getinfo($ch);
// Merge headers into one array
$this->response_meta_info = array_merge($headers, $this->response_meta_info);
// Close the curl session
curl_close($ch);
// Cache feed
file_put_contents($filename, $output);
// XML to object
$output = simplexml_load_string($output);
// Return the output as an array
return $output;
}
It returns the following http headers
Array(
[url] => http://example.com
[content_type] => text/xml
[http_code] => 200
[header_size] => 425
[request_size] => 108
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.677605
[namelookup_time] => 0.181104
[connect_time] => 0.342955
[pretransfer_time] => 0.343036
[size_upload] => 0
[size_download] => 6579
[speed_download] => 9709
[speed_upload] => 0
[download_content_length] => 6579
[upload_content_length] => 0
[starttransfer_time] => 0.515733
[redirect_time] => 0
[expires] => Wed, 23 Nov 2011 15:49:24 GMT
)
I want to use the [expires] => Wed, 23 Nov 2011 15:49:24 GMT
header instead of using the time now minus 30 seconds bit of this if statement (file_exists($filename) && (filemtime($filename) > time() - 30)) {
as then it will only update the cache when it needs to.
But I can't work out how to do this.
Upvotes: 0
Views: 741
Reputation: 47321
You can touch()
the xml file with the timestamp return by strtotime
,
example :-
date_default_timezone_set('Asia/Singapore'); // replace to your timezone
$time = strtotime('Wed, 23 Nov 2011 15:49:24 GMT');
// after file_put_contents
// call this to change the time
touch($filename, $time);
Upvotes: 1