7

I want to get last modification time of the remote file. I am using this code I found here on stackoverflow

$curl = curl_init();

    curl_setopt($curl, CURLOPT_URL,$url);
    //don't fetch the actual page, you only want headers
    curl_setopt($curl, CURLOPT_NOBODY, true);
    curl_setopt($curl, CURLOPT_HEADER, 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);
    echo $result;
    $info = curl_getinfo($curl);
    print_r($info);
    if ($info['filetime'] != -1) { //otherwise unknown
        echo date("Y-m-d H:i:s", $info['filetime']); //etc
    }  

Problem with this code I am getting filetime = -1 all the time. But when I delete

curl_setopt($curl, CURLOPT_NOBODY, true);

then I am getting correct modification time.

Is it possible to get last modification time but with

curl_setopt($curl, CURLOPT_NOBODY, true);

included in the script. I just need header of the page, not body.

Thanks in advance

kukipei
  • 631
  • 2
  • 8
  • 32
  • Do you get any actual Header info back? Some servers may be (mis)configured not to respond to a HEAD request. – Devin Ceartas Oct 10 '11 at 04:20
  • Yes, I am getting headers info but only when there isn't `curl_setopt($curl, CURLOPT_NOBODY, true);` in the code. – kukipei Oct 10 '11 at 07:07

3 Answers3

4

Given the added information in our Q/A discussion, it sure sounds like you're just not getting a response. It could be that the server is configured with some sort of which intentionally or inadvertently blocks HEAD requests for some reason, or there could be a difficult proxy involved.

When I'm debugging PHP cURL stuff, I often find it useful to use a *nix box (my mac, or ssh to a server) and run the requests from the command line, so I can see the result without worrying about if the PHP is doing the right thing, until I get the cURL part working. For instance:

$ curl --head stackoverflow.com

HTTP/1.1 200 OK
Cache-Control: public, max-age=49
Content-Length: 190214
Content-Type: text/html; charset=utf-8
Expires: Mon, 10 Oct 2011 07:22:07 GMT
Last-Modified: Mon, 10 Oct 2011 07:21:07 GMT
Vary: *
Date: Mon, 10 Oct 2011 07:21:17 GMT
Devin Ceartas
  • 4,743
  • 1
  • 20
  • 33
1

Based on this solution
Remote file size without downloading file

function retrieve_remote_file_time($url) {
    $ch = curl_init($url);

     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
     curl_setopt($ch, CURLOPT_HEADER, TRUE);
     curl_setopt($ch, CURLOPT_NOBODY, TRUE);
     curl_setopt($ch, CURLOPT_FILETIME, TRUE);

     $data = curl_exec($ch);
     $filetime = curl_getinfo($ch, CURLINFO_FILETIME);

     curl_close($ch);

     return $filetime;
}
Kerem
  • 11,377
  • 5
  • 59
  • 58
lulu
  • 11
  • 1
0

I'm going to take a punt and say that the server you're connecting to might be an IIS web server.

In my case, I've found that the IIS 7 server I'm connecting to does NOT return a Last-Modified date when I issue a HEAD request via Curl using PHP (but it does return the Last-Modified when doing a usual GET request).

If you are in control over the server you're connecting to, see if you can get the web server to correctly issue the Last-Modified date. Otherwise don't use CURLOPT_NOBODY.

Gavin G
  • 856
  • 6
  • 6