7

リモートファイルの最終変更時刻を取得したい。ここで見つけたこのコードを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
    }  

このコードの問題私は常に filetime = -1 を取得しています。しかし、私が削除するとき

curl_setopt($curl, CURLOPT_NOBODY, true);

それから私は正しい修正時間を取得しています。

最終変更時刻を取得することは可能ですが、

curl_setopt($curl, CURLOPT_NOBODY, true);

スクリプトに含まれています。本文ではなく、ページのヘッダーだけが必要です。

前もって感謝します

4

3 に答える 3

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
于 2011-10-10T07:22:52.577 に答える
0

パントを取り、接続しているサーバーが IIS Web サーバーである可能性があると言います。

私の場合、PHP を使用して Curl 経由で HEAD リクエストを発行すると、接続している IIS 7 サーバーが Last-Modified 日付を返さないことがわかりました (ただし、通常の GET を実行すると Last-Modified が返されます)リクエスト)。

接続しているサーバーを制御している場合は、Web サーバーに Last-Modified 日付を正しく発行させることができるかどうかを確認してください。それ以外の場合は、CURLOPT_NOBODY を使用しないでください。

于 2012-05-11T06:59:04.553 に答える