【问题标题】:Last-Modified to check feed is updated since last downloadLast-Modified 检查提要自上次下载后是否更新
【发布时间】:2013-11-10 08:53:41
【问题描述】:

我使用 PHP 每半小时读取一次提要 xml,到目前为止,我一直在读取整个提要文件,无论它是否更新。但我想检查自上次下载以来提要 xml 是否更新,如果更新则只读取 xml,否则不读取。

我正在尝试使用下面的代码来实现它,但 $lastmodifiedRSS 始终为空。我不确定我的代码出了什么问题。如果我得到 $lastmodifiedRSS 那么我可以很容易地将它与上次加载时间进行比较,然后决定做什么。

如果有专家可以分享一些信息,那就太好了。提前谢谢你。

//get feed information & content
$feed = new feed($rssurl);
$feed->load();

//get the last modified date (web server information)
$lastmodifiedRSS = $feed->http->get_header_value('Last-Modified');

        function http($url)
{
    $this->url($url);
    $this->user_agent = 'posh';
    $this->header = "";
    $this->code = "";
    $this->codeStr = "";
    $this->max_d = 5;
    $this->authorization = "";
    $this->proxy_auth = "";
    $this->url = $url;
    $this->addProxy();
}

    function feed($url)
{
    if ($url) {
        $this->url = $url;
        $this->http = new http($url);
        //$this->addProxy();
    } else {
        $this->http = new http('');      
    }
}
function load()
    {
        $content= $this->http->get();
        if (!$content)
            return false;
        $content = $this->transcode($content);
        return $content;
    }

function get_header_value($name)
    {
        if (preg_match("/$name: ?([^\\r\\n]*)\\r\\n/m",$this->head,$matches) !== false)
        {
            if (count($matches) > 1)
            {
                return $matches[1];
            }
        }
        return false;
    }

问候, 莫娜

【问题讨论】:

    标签: php rss-reader feed feedparser


    【解决方案1】:

    在 PHP 中使用 stat()

    <?php
    print_r(stat('users.json'));
    

    输出:

    Array ( [0] => 2 [1] => 0 [2] => 33206 [3] => 1 [4] => 0 [5] => 0 [6] => 2 [7] => 298 [8] => 1384073940 [9] => 1384073940 [10] => 1368626190 [11] => -1 [12] => -1 [dev] => 2 [ino] => 0 [mode] => 33206 [nlink] => 1 [uid] => 0 [gid] => 0 [rdev] => 2 [size] => 298 [atime] => 1384073940 [mtime] => 1384073940 [ctime] => 1368626190 [blksize] => -1 [blocks] => -1 )
    

    Source

    跟踪变量 [atime] , [size] 可以帮助您实现您想要做的事情。

    【讨论】:

    • 嗨 Shankar,谢谢你的回答.. 是那个 mtime 会给我提要文件修改时间......所有它都可以在远程文件上工作......
    • 是的,莫娜是正确的。但它会返回一个 UNIX 时间戳。
    【解决方案2】:

    这种检查最好使用 HTTP 标头本身来处理。

    如果服务器在响应中发送Etag 标头,您可以在下一个请求中将其与If-None-Match 标头一起使用,以了解文件是否已更改。

    同样,如果服务器在响应中发送Last-Modified 标头,您可以在下一个请求中使用此时间戳和If-Modified-Since 标头来了解文件是否已更改。

    只是为了让你知道你能做什么,我不能提供任何代码,因为我不知道你在这里使用的是哪个 HTTP 库。

    【讨论】:

      猜你喜欢
      • 2018-01-29
      • 2017-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-03
      • 1970-01-01
      • 2015-11-18
      • 2014-05-25
      相关资源
      最近更新 更多