【问题标题】:Get the last modified date of a remote file获取远程文件的最后修改日期
【发布时间】:2009-05-10 12:12:10
【问题描述】:

我想通过 curl 获取远程文件的最后修改日期。有谁知道这是怎么做到的吗?

【问题讨论】:

    标签: php jquery curl


    【解决方案1】:

    您可能可以使用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
    } 
    

    【讨论】:

    • 我注意到此代码有时不起作用,本机 php get_headers 对我来说效果更好。
    【解决方案2】:

    在 PHP 中你可以使用原生函数get_headers():

    <?php
    $h = get_headers($url, 1);
    
    $dt = NULL;
    if (!($h || strstr($h[0], '200') === FALSE)) {
        $dt = new \DateTime($h['Last-Modified']);//php 5.3
    }
    

    【讨论】:

    • “如果”条件不能正常工作...if(!$h || strpos($h[0], '200') !== false){ 更适合我!
    • 恐龙是正确的。看起来上面的代码中意外省略了! 运算符。 if 语句应该是if (!(!$h || strstr($h[0], '200') === FALSE)) {
    • 可能还想将 Pons 中的小写代码合并并添加到此。 if(strtolower(trim($k))=='last-modified')
    【解决方案3】:

    来自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() 是这里的关键。但我不确定您是否可以获取 remote 文件的最后修改日期,因为服务器应该将其发送给您...也许在 HTTP 标头中?

    【讨论】:

    • 来自手册:“从 PHP 5.0.0 开始,这个函数也可以与 一些 URL 包装器一起使用。”
    • 根据我的经验,这种方法并不总是有效(这取决于您的 php.ini),因此本机 get_headers 对我来说效果更好。
    • filemtime() 仅适用于同一服务器上的文件。使用 $ch = curl_init("domian.com/dir/file.htm"); curl_setopt($ch, CURLOPT_NOBODY, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FILETIME, true); $result = curl_exec($ch) ; $retcode= curl_getinfo($ch, CURLINFO_FILETIME); curl_close($ch);
    • 为什么这被标记为正确答案?正如@MarkAntonyAgius 所说,filemtime() 不适用于远程文件。
    【解决方案4】:

    有时标题带有不同的大写小写,这应该会有所帮助:

    function remoteFileData($f) {
        $h = get_headers($f, 1);
        if (stristr($h[0], '200')) {
            foreach($h as $k=>$v) {
                if(strtolower(trim($k))=="last-modified") return $v;
            }
        }
    }
    

    【讨论】:

      【解决方案5】:

      您可以使用curl_setopt($handle, CURLOPT_HEADER, true) 激活接收回复的标题。您还可以打开 CURLOPT_NOBODY 以仅接收标头,然后将结果分解为 \r\n 并解释单个标头。标题Last-Modified 是您感兴趣的标题。

      【讨论】:

      • 是的 - 只需请求标头
      • 假设他们发送 Last-Modified 标头
      • 这似乎与 403 有问题 - 禁止 - 响应 - 它似乎无法访问标题。不要认为有办法解决这个问题。
      【解决方案6】:

      通过编辑 h4kuna 的答案,我创建了这个:

      $fileURL='http://www.yahoo.com';
      $headers = get_headers($fileURL, 1);
      $date = "Error";
      //echo "<pre>"; print_r($headers); echo "</pre>";
      if ( $headers && (strpos($headers[0],'200') !== FALSE) ) {
          $time=strtotime($headers['Last-Modified']);
          $date=date("d-m-Y H:i:s", $time);
      }
      echo 'file: <a href="'.$fileURL.'" target="_blank">'.$fileURL.'</a> (Last-Modified: '.$date.')<br>';
      

      【讨论】:

        【解决方案7】:

        像这样的工作,来自web developer forum

        <? $last_modified = filemtime("content.php"); print("Last Updated - ");
        print(date("m/d/y", $last_modified)); ?
        
        // OR
        
        $last_modified = filemtime(__FILE__); 
        

        该链接提供了一些有用的网站,您可以使用它们

        【讨论】:

          【解决方案8】:

          必须解决类似的问题,但对我来说每天下载一次就足够了,所以我只比较了本地(下载)缓存文件的修改日期。远程文件没有 Last-Modified 标头。

          $xml = 'test.xml';
          if (is_file($xml) || date('d', filemtime($xml)) != date('d')) {
              $xml = file_get_contents(REMOTE_URL);
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-10-17
            • 2012-12-06
            • 2019-02-26
            • 2010-09-17
            • 1970-01-01
            相关资源
            最近更新 更多