How can I use HTTP headers to control when my cache should be updated?
At the moment I am using cURL to grab a live XML feed and then save it into an xml file. The feed also sends HTTP headers notifying you of when it will be updated. The update can be anywhere between 30seconds and 3mins.
The header looks like this
Expires: Mon, 22 Nov 2011 10:01:22 GMT
and this is what I am currently using to check every 30seconds
if (file_exists($filename) && (filemtime($filename) > time() - 30)) {
I would prefer it to only update based on what the HTTP headers say.
How would I go about doing so?
Also is there a better way of caching this XML feed rather than saving it to a XML file?
first of all you will have to parse the date given in the headers:
$header = "Expires: Mon, 22 Nov 2011 10:01:22 GMT";
preg_match(
"/Expires: [A-Za-z]+, ([0-9]{1,2}) ([A-Za-z]+) ([0-9]{4}) ([0-9:]+) ([A-Z]{3})/",
$header,
$matches
);
$months = array(
"Jan" => "01",
"Feb" => "02",
"Mar" => "03",
"Apr" => "04",
"May" => "05",
"Jun" => "06",
"Jul" => "07",
"Aug" => "08",
"Sep" => "09",
"Oct" => "10",
"Nov" => "11",
"Dec" => "12"
);
$day = $matches[1];
$month = $months[$matches[2]];
$year = $matches[3];
$time = $matches[4];
$zone = $matches[5];
$date = new DateTime("$year-$month-$day $time", new DateTimeZone($zone));
then you can check this against the actual time and only execute the update if the $date from the last update is reached
$now = new DateTime();
if($now > $date);
you should save the $date in a file or DB after you downloaded the XML and parsed the date. next time you execute the script just check the saved date against the new DateTime() to see if you already have to update it, if not you can load the saved XML from the file.
saving the XML as file is good, you might save it to a Database, but I would not set up a DB only for saving one XML-Structure. If you want to keep a history of old XMLs the DB makes sense again.