开发者

Generating cache file for Twitter rss feed

开发者 https://www.devze.com 2023-01-04 04:50 出处:网络
I\'m working on a site with a simple php-generated twitter box with user timeline tweets pulled from the user_timeline rss feed, and cached to a local file to cut down on loads, and as backup for when

I'm working on a site with a simple php-generated twitter box with user timeline tweets pulled from the user_timeline rss feed, and cached to a local file to cut down on loads, and as backup for when twitter goes down. I based the caching on this: http://snipplr.com/view/8156/twitter-cache/. It all seemed to be working well yesterday, but today I discovered the cache file was blank. Deleting it then loading again generated a fresh file.

The code I'm using is below. I've edited it to try to get it to work with what I was already using to display the feed and probably messed something crucial up.

The changes I made are the following (and I strongly believe that any of these could be the cause): - Revised the time difference code (the linked example seemed to use a custom function that wasn't included in the code)

  • Removed the "serialize" function from the "fwrites". This is purely because I couldn't figure out how to unserialize once I loaded it in the display code. I truthfully don't understand the role that serialize plays or how it works, so I'm sure I should have kept it in. If that's the case I just need to understand where/how to deserialize in the second part of the code so that it can be parsed.

  • Removed the $rss variable in favor of just loading up the cache file in my original tweet display code.

So, here are the relevant parts of the code I used:

<?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 Nobody can help…?

I'm thinking it has something to do with writing a blank cache file over a good one when twitter is down, because otherwise I imagine that this should be happening every ten minutes when the cache file is overwritten again, but it doesn't happen that frequently.

I made the following change to the part where it checks how old the file is to ove开发者_JAVA百科rwrite it:

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

…so now, it will only write the file if it's over ten minutes old and there's actual content retrieved from the rss page. Do you think this will work?


Yes your code is problematic, because whatever Twitter sends you, you write it. You should test the file you get from Twitter like this:

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

file_get_contents() return false if there is an error, check it before caching some new content.

0

精彩评论

暂无评论...
验证码 换一张
取 消