PHP远程文件最后修改时间

发布于 2024-12-09 01:54:58 字数 987 浏览 0 评论 0原文

我想获取远程文件的最后修改时间。我正在使用我在 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);

包含在脚本中。 我只需要页面标题,而不是正文。

提前致谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遗忘曾经 2024-12-16 01:54:58

鉴于我们的问答讨论中添加的信息,听起来您确实没有得到回复。可能是服务器配置了某种出于某种原因有意或无意地阻止 HEAD 请求的服务器,或者可能涉及到困难的代理。

当我调试 PHP cURL 内容时,我经常发现使用 *nix 盒子(我的 mac 或 ssh 到服务器)并从命令行运行请求非常有用,这样我就可以看到结果,而不必担心是否PHP 正在做正确的事情,直到我让 cURL 部分工作。例如:

$ 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

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
当梦初醒 2024-12-16 01:54:58

基于此解决方案
不下载文件的远程文件大小

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;
}

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;
}
兮子 2024-12-16 01:54:58

我打算冒险说您正在连接的服务器可能是 IIS Web 服务器。

就我而言,我发现当我使用 PHP 通过 Curl 发出 HEAD 请求时,我连接到的 IIS 7 服务器不会返回上次修改日期(但在执行常规 GET 操作时它确实返回上次修改日期)要求)。

如果您可以控制要连接的服务器,请查看是否可以让 Web 服务器正确发出上次修改日期。否则不要使用 CURLOPT_NOBODY。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文