tags:

views:

59

answers:

5

Instead of the file itself?

EDIT

Best with a demo in PHP?

A: 

You need to write some service to handle the http request and extract the last modified date for the requested file and return the date in response.

Bhushan
I think he was asking how...
spoulson
+1  A: 

In your HTTP request you should add any of these header attributes, and you may receive an 304 (Last modified)

  1. If-Modified-Since
  2. If-None-Match
  3. If-Unmodified-Since

Andrei is correct, HEAD will only get the headers. My suggestion will return only the header and no body if the condictions are met. If the content has been updated the body will contain the new information.

monksy
If-Modified-Since and a date in the future (next year) should prevent you from ever getting the file in a response. This can be critical when the file your checking is large ie: > 100MB
PHP-Steven
What if the webserver is located in the future? :P... in all seriousness: Thats awesome.
monksy
+4  A: 

Yes. The "HEAD" method returns only the response headers and not the actual data.

Andrei
I tried to use this for something different, and encountered a number of servers that did not respond to the HEAD method, and treated it like a GET. Just a warning.
Kenny Winker
@Kenny: I suspect this is to prevent DDOS attacks on just the header information.
monksy
+1  A: 
<?php
// Get Headers
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,            'http://example.com/'); 
curl_setopt($ch, CURLOPT_HEADER,         true); 
curl_setopt($ch, CURLOPT_NOBODY,         true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_TIMEOUT,        10); 
$response = curl_exec($ch); 

// Process Headers
$headerLines = explode("\r\n", $response);
foreach ($headerLines as $headerLine) {
    $headerLineParts = explode(': ', $headerLine);
    if (count($headerLineParts) >= 2) {
     $headers[$headerLineParts[0]] = $headerLineParts[1];
    }
}

echo $headers['Last-Modified'];
?>
philfreo
CURLOPT_NOBODY is the critical part here as it sets the method to HEAD instead of GET. Otherwise you will still get the full file and it will be tossed into the bit bucket (aka: /dev/null )
PHP-Steven
You mean CURLOPT_HEADER won't work alone?
Mask
No... CURLOPT_HEADER means "include the header" whereas CURLOPT_NOBODY means "don't include the body". You need both if you really just want the header, for speed.
philfreo
+2  A: 

You can use php's get_headers function

$a = get_headers('http://sstatic.net/so/img/logo.png');
print_r($a);
Galen
+1,also works .
Mask