Skip to content

Instantly share code, notes, and snippets.

@fitzhaile fitzhaile/*.php
Created Sep 26, 2012

Embed
What would you like to do?
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
You can’t perform that action at this time.