繁体   English   中英

为Twitter RSS提要生成缓存文件

[英]Generating cache file for Twitter rss feed

我正在一个带有由php生成的简单Twitter框的网站上,该框具有从user_timeline rss feed提取的用户时间轴tweet,并缓存到本地文件以减少负载,并作为twitter下降时的备份。 我基于此进行缓存: http : //snipplr.com/view/8156/twitter-cache/ 昨天一切似乎运行良好,但是今天我发现缓存文件为空。 将其删除然后再次加载会生成一个新文件。

我正在使用的代码如下。 我已经对其进行了编辑,以使其能够与已经用于显示提要的东西一起使用,并且可能弄乱了一些关键内容。

我所做的更改如下(并且我坚信,其中任何一个都可能是原因):-修改了时差代码(链接的示例似乎使用了代码中未包含的自定义函数)

  • 从“ fwrites”中删除了“ serialize”功能。 这纯粹是因为一旦将其加载到显示代码中,我就无法弄清楚如何反序列化。 我确实不了解序列化扮演的角色或它如何工作,所以我确定我应该保留它。如果是这种情况,我只需要了解代码第二部分中在何处/如何反序列化,就可以了。它可以被解析。

  • 删除了$ rss变量,仅在我的原始tweet显示代码中加载了缓存文件。

因此,这是我使用的代码的相关部分:

<?php
$feedURL = "http://twitter.com/statuses/user_timeline/#######.rss";

// START CACHING
$cache_file = dirname(__FILE__).'/cache/twitter_cache.rss';
    // Start with the cache 
if(file_exists($cache_file)){
 $mtime = (strtotime("now") - filemtime($cache_file));
 if($mtime > 600) {
  $cache_rss = file_get_contents('http://twitter.com/statuses/user_timeline/75168146.rss');
  $cache_static = fopen($cache_file, 'wb');
  fwrite($cache_static, $cache_rss);
  fclose($cache_static);  
 }
 echo "<!-- twitter cache generated ".date('Y-m-d h:i:s', filemtime($cache_file))." -->";
}
else {
 $cache_rss = file_get_contents('http://twitter.com/statuses/user_timeline/#######.rss');
 $cache_static = fopen($cache_file, 'wb');
 fwrite($cache_static, $cache_rss);
 fclose($cache_static);
}
//END CACHING

//START DISPLAY
   $doc = new DOMDocument();
 $doc->load($cache_file);
 $arrFeeds = array();
 foreach ($doc->getElementsByTagName('item') as $node) {
     $itemRSS = array ( 
         'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
         'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
         );
     array_push($arrFeeds, $itemRSS);
    }
 // the rest of the formatting and display code....
 }
?>

ETA 6/17没有人可以帮助…?

我认为这与在Twitter处于关闭状态时在一个好的缓存文件上写入一个空白缓存文件有关,因为否则我想当缓存文件再次被覆盖时,应该每隔十分钟发生一次,但是这种情况不会发生如此频繁。

我对检查文件覆盖它的年龄的部分进行了以下更改:

 $cache_rss = file_get_contents('http://twitter.com/statuses/user_timeline/75168146.rss');
 if($mtime > 600 && $cache_rss != ''){
   $cache_static = fopen($cache_file, 'wb');
   fwrite($cache_static, $cache_rss);
   fclose($cache_static);  
 }

…所以现在,仅当文件已超过十分钟并且从rss页面检索到实际内容时,它才会写入文件。 您认为这行得通吗?

是的,您的代码有问题,因为无论Twitter发送给您什么,您都可以编写。 您应该像这样测试从Twitter获得的文件:

if (($mtime > 600) && ($cache_rss = file_get_contents($feedURL)))
{
  file_put_contents($cache_rss);
}

如果发生错误,file_get_contents()返回false,请在缓存一些新内容之前检查它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM