Created
September 26, 2012 15:27
-
-
Save fitzhaile/3788700 to your computer and use it in GitHub Desktop.
PHP: Get Twitter Timeline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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