2

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

AndreLiem
  • 2,031
  • 5
  • 20
  • 26

2 Answers2

5

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
  • 316,276
  • 54
  • 369
  • 333
  • +1, but doesn't Last-Modified come from a and thus could be inaccurate? – Dana the Sane Nov 21 '08 at 19:20
  • 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 Nov 21 '08 at 19:25
1

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
  • 2,031
  • 5
  • 20
  • 26