Try sending a HEAD request first to get the last-modified header for the destination URL to compare your cached version. You can also try using the If-Modified-Since header over time, when your cached version will be created using a GET request so that the other side can also respond to you with 302 Not Modified .
Sending a HEAD request using curl looks something like this:
$curl = curl_init($url); curl_setopt($curl, CURLOPT_NOBODY, true); curl_setopt($curl, CURLOPT_HEADER, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1); $content = curl_exec($curl); curl_close($curl)
Now $content will contain the returned HTTP header, as one long string, you can search last-modified: as follows:
if (preg_match('/last-modified:\s?(?<date>.+)\n/i', $content, $m)) { // the last-modified header is found if (filemtime('your-cached-version') >= strtotime($m['date'])) { // your cached version is newer or same age than the remote content, no re-fetch required } }
You should also handle the expires header (extract the value from the title bar, check if the value is in the future or not)
source share