views:

119

answers:

3

Is there a basic php method that accepts a URL and retrieves the date last modified from the header?

It would seem like something php can do, but I'm not sure which object to check.

Thanks

+5  A: 

Give this a go.. using cURL.

$c = curl_init('http://...');
curl_setopt($c, CURLOPT_HEADER, 1); // Include the header
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); // Return the result instead of printing it
$result = curl_exec($c);

if (curl_errno($c))
    die(curl_error($c));

// $result now contains the response, including the headers

if (preg_match('/Last-Modified:(.*?)/i', $result, $matches))
    var_dump($matches[1]);
Greg
+1, but doesn't Last-Modified come from a <meta> and thus could be inaccurate?
Dana the Sane
They come from the web server. Of course for a dynamic file (e.g. PHP, ASP) the header may be inaccurate or not present at all, but that's just something you have to deal with.
Greg
A: 

thanks for the code snippet... I tried it but it returns the actual url to the browser and loads it. Although I think with this as as start I may be able to figure out the rest.

thanks again!

AndreLiem
Oops, the CURL_... options should read CURLOPT_.... I've edited the post
Greg
+1  A: 

Thanks... I tried modifying your version a bit and this seems to work for me:

$c = curl_init('http://...');    
curl_setopt($c, CURLOPT_HEADER, 1); // Include the header    
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_FILETIME, 1);   
curl_exec($c);
$result = curl_getinfo($c);   

if (curl_errno($c))
    die(curl_error($c));

echo date('G:i M jS \'y',(int)$result['filetime']);
AndreLiem