I would like to get the last modified date of a remote file by means of curl. Does anyone know how to do that?
From php's article:
<?php
// outputs e.g. somefile.txt was last modified: December 29 2002 22:16:23.
$filename = 'somefile.txt';
if (file_exists($filename)) {
echo "$filename was last modified: " . date ("F d Y H:i:s.", filemtime($filename));
}
?>
filemtime() is the key here. But I'm not sure if you can get the last modified date of a remote file, since the server should send it to you... Maybe in the HTTP headers?
You could probably do something like this using curl_getinfo()
:
<?php
$curl = curl_init('http://www.example.com/filename.txt');
//don't fetch the actual page, you only want headers
curl_setopt($curl, CURLOPT_NOBODY, true);
//stop it from outputting stuff to stdout
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// attempt to retrieve the modification date
curl_setopt($curl, CURLOPT_FILETIME, true);
$result = curl_exec($curl);
if ($result === false) {
die (curl_error($curl));
}
$timestamp = curl_getinfo($curl, CURLINFO_FILETIME);
if ($timestamp != -1) { //otherwise unknown
echo date("Y-m-d H:i:s", $timestamp); //etc
}
would something like this work, from web developer forum
<? $last_modified = filemtime("content.php"); print("Last Updated - ");
print(date("m/d/y", $last_modified)); ?
// OR
$last_modified = filemtime(__FILE__);
the link provides some useful insite on you can use them
You can activate receiving the headers of the reply with curl_setopt($handle, CURLOPT_HEADER, true)
. You can also turn on CURLOPT_NOBODY to only receive the headers, and after that explode the result by \r\n and interpret the single headers. The header Last-Modified
is the one that you are interested in.
By setting the CURLOPT_FILETIME option first when you request the page, then you use curl_easy_getinfo() after it's done with the CURLINFO_FILETIME option to extract it.
Then you won't need to parse any headers or convert a string to a time.