开发者

Check if remote file has changed

开发者 https://www.devze.com 2023-04-08 19:38 出处:网络
I\'m using PHP cURL module to extract timestamp of a remote file via HTTP headers. I\'ve managed to grab modification timestamp by using CURLOPT_FILETIME constant. Of course, I\'m doing this in order

I'm using PHP cURL module to extract timestamp of a remote file via HTTP headers. I've managed to grab modification timestamp by using CURLOPT_FILETIME constant. Of course, I'm doing this in order too see if the remote file has changed without downloading it's contents.

$ch = curl_init($url);  /* create URL handler */
curl_setopt($ch, CURLOPT_NOBODY, TRUE); /* don't retrieve body contents */
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); /* follow redirects */
curl_setopt($ch, CU开发者_StackOverflow社区RLOPT_HEADER, FALSE); /* retrieve last modification time */
curl_setopt($ch, CURLOPT_FILETIME, TRUE); /* get timestamp */
$res = curl_exec($ch);
$timestamp = curl_getinfo($ch, CURLINFO_FILETIME);
curl_close($ch);

What is, in your opinion the best way to check if remote file has changed? Should I go with timestamp check only? Or are there some other clever options that didn't came to my mind?!


Your approach looks good for finding the Last-Modified time value. (Need to watch out for -1 return for CURLINFO_FILETIME, meaning that no Last-Modified header value was identified.)

You could save the time returned, and in future checks see if it has changed. If it's changed, fetch the new file via Curl.

Another option would be to save ETag and Last-Modified headers, then use a conditional request to get the image again. This would be more complex, but you'd save the additional HEAD request each time. You can see some of the details in this SO question: if-modified-since vs if-none-match.

0

精彩评论

暂无评论...
验证码 换一张
取 消