Skip to content

Instantly share code, notes, and snippets.

@roelven
Created April 19, 2010 12:29
Show Gist options
  • Save roelven/370987 to your computer and use it in GitHub Desktop.
Save roelven/370987 to your computer and use it in GitHub Desktop.
Queries the TweetMeme API to check for tweets about this url
<?php
// Queries the TweetMeme API to check for tweets about this url
// If no results, TweetMeme will create a story for this link
// Uses the get_url function: http://gist.github.com/370985
// Make sure you set the right permissions for you cache directory!
function tweetMeme($bloglink) {
// Set some vars:
$tweetmemeAPI = 'http://api.tweetmeme.com/url_info?url=';
$cache_dir = '/usr/local/www/cache/';
$expire = 2; // hours
$url = $tweetmemeAPI.$bloglink;
$current_time = time();
$expire_time = $expire * 60 * 60;
$file = $cache_dir . 'twtmeme_' . md5($bloglink);
if(!file_exists($file)) {
file_put_contents($file, '');
};
$file_time = filemtime($file);
// If file exists, and when it's not expired, AND when it's not empty, return contents:
if(file_exists($file) && ($current_time - $expire_time < $file_time) && file_get_contents($file) != '') {
return file_get_contents($file);
} else {
// Retrieve fresh content, parse XML and write to file (and log):
$vars = new SimpleXMLElement(get_url($url));
if (!$vars->story->url_count) {
$tweetamount = '0';
$content = $tweetamount;
$content.= '<!-- cached: '.time().'-->';
} else {
$tweetamount = $vars->story->url_count;
$memelink = $vars->story->tm_link;
$content = '<a href="'.$memelink.'" target="_blank">'.$tweetamount.'</a>';
$content.= '<!-- cached: '.time().'-->';
}
file_put_contents($file, $content);
// Optional, use for debugging / stats:
error_log('Tweetmeme pinged for '.$bloglink.':: '.$content);
return $content;
};
};
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment