Skip to content

Instantly share code, notes, and snippets.

@codepotato
Last active December 13, 2015 19:09
Show Gist options
  • Save codepotato/4960925 to your computer and use it in GitHub Desktop.
Save codepotato/4960925 to your computer and use it in GitHub Desktop.
Simple Twitter Feed display with caching.
<?php
$twitter_name = '';
$number_of_tweets = 2;
$url = "https://api.twitter.com/1/statuses/user_timeline/$twitter_name.xml?count=$number_of_tweets&include_rts=1callback=?";
$cache_expire = 3600; // in seconds
$ts = time();
$info_file = 'tmp-info.txt';
$cache_file = 'tmp-'.$ts.'.xml';
// current info
$info = unserialize(@file_get_contents($info_file));
if (empty($info) OR $ts > ($info['cache_ts'] - $cache_expire))
{
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
curl_close($ch);
// if a description tag is present we're OK
if (preg_match('/<description>/iS',$content))
{
file_put_contents($cache_file,$content);
@unlink($info['cache_file']);
}
// known error strings: "over capacity", "rate limit exceeded"
// else if a description tag is not present something is wrong
else
{
// use current cache until errors resolve itself
$cache_file = $info['cache_file'];
$content = file_get_contents($info['cache_file']);
}
// update next cache time and cache file name
file_put_contents($info_file,serialize(array('cache_ts'=>$ts,'cache_file'=>$cache_file)));
}
else
{
$content = file_get_contents($info['cache_file']);
}
$feed = array();
$doc = new DOMDocument();
$doc->loadXML($content);
foreach ($doc->getElementsByTagName('status') as $node)
{
array_push($feed, array
(
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => preg_replace('/^w :/i','',$node->getElementsByTagName('text')->item(0)->nodeValue),
'link' => 'http://www.twitter.com/' . $twitter_name . '/status/' . $node->getElementsByTagName('id')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('created_at')->item(0)->nodeValue
));
}
function relativedate($secs) {
$second = 1;
$minute = 60;
$hour = 60*60;
$day = 60*60*24;
$week = 60*60*24*7;
$month = 60*60*24*7*30;
$year = 60*60*24*7*30*365;
if ($secs <= 0)
{
$output = "now";
}
elseif ($secs > $second && $secs < $minute)
{
$output = round($secs/$second)." second";
}
elseif ($secs >= $minute && $secs < $hour)
{
$output = round($secs/$minute)." minute";
}
elseif ($secs >= $hour && $secs < $day)
{
$output = round($secs/$hour)." hour";
}
elseif ($secs >= $day && $secs < $week)
{
$output = round($secs/$day)." day";
}
elseif ($secs >= $week && $secs < $month)
{
$output = round($secs/$week)." week";
}
elseif ($secs >= $month && $secs < $year)
{
$output = round($secs/$month)." month";
}
elseif ($secs >= $year && $secs < $year*10)
{
$output = round($secs/$year)." year";
}
else
{
$output = " more than a decade ago";
}
if ($output <> "now"){
$output = (substr($output,0,2)<>"1 ") ? $output."s" : $output;
}
return $output;
}
foreach($feed as $row)
{
echo('<div>'.$row['desc'].'<span><a href="'.$row['link'].'">'.relativedate(time()-strtotime($row['date'])).' ago</a></span></div>');
}
?>
@MantisSTS
Copy link

Just fyi line 17 should be:

if (empty($info) OR $ts > ($info['cache_ts'] + $cache_expire)) // Note the plus instead of the minus.

Otherwise the Cache always expires and you will always be polling for the new version.

Credit to Kevin Relland for noticing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment