Skip to content

Instantly share code, notes, and snippets.

@fitzhaile
Created September 26, 2012 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fitzhaile/3788700 to your computer and use it in GitHub Desktop.
Save fitzhaile/3788700 to your computer and use it in GitHub Desktop.
PHP: Get Twitter Timeline
/**
* Simple Twitter status getter.
* @param string $handle User Twitter handle
* @param integer $count Desired number of tweets
* @return array Indexed array of tweets
*/
function dev_get_twitter_timeline($handle, $count = 3) {
// WP template tag
$save = get_stylesheet_directory() . '/assets/twitter.json';
$time = time();
$age = $time - @filemtime($save);
$cache_life = 5 * 60; // 5 minutes
if( !file_exists($save) or $age >= $cache_life )
{
$get = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' . $handle . '&count=' . $count;
$json = file_get_contents($get);
file_put_contents($save, $json);
}
$string = file_get_contents($save);
if($tweets = json_decode($string))
{
$statuses = array();
foreach( $tweets as $tweet )
{
$statuses[] = linkify_twitter_status($tweet->text);
}
return $statuses;
}
else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment