我使用cURL获取XML提要并将其保存到XML文件中,如下所示
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;
}
返回以下http报头
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
)
我想使用[expires] => Wed, 23 Nov 2011 15:49:24 GMT
标头,而不是使用这个if语句(file_exists($filename) && (filemtime($filename) > time() - 30)) {
现在减去30秒的时间,因为它只会在需要时更新缓存。
但是我不知道怎么做。
您可以用strtotime
返回的时间戳来touch()
xml文件,
例子:-
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);