Return to Snippet

Revision: 54138
at December 11, 2011 01:03 by Sverri


Initial Code
function myTweets($screenname, $count=5, $simple=TRUE) {
	$url = "http://twitter.com/statuses/user_timeline.json?screen_name={$screenname}&count={$count}&include_rts=1";
	$data = @file_get_contents($url);
	if ($http_response_header[0] != 'HTTP/1.0 200 OK'):
		return FALSE;
	endif;
	$data = json_decode($data);
	if ($simple == TRUE):
		$out = array();
		foreach ($data as $tweet):
			$o = new stdClass;
			$o->text = htmlentities($tweet->text, ENT_QUOTES, 'UTF-8', FALSE);
			$o->id = $tweet->id_str;
			$o->timestamp = strtotime($tweet->created_at);
			$o->url = "https://twitter.com/#!/{$screenname}/status/{$tweet->id_str}";
			$out[] = $o;
		endforeach;
		$data = $out;
	endif;
	return $data;
}

$tweets = myTweets('sverrimo', 2);
echo '<pre>', print_r($tweets);

Initial URL

                                

Initial Description
A simple PHP function for fetching tweets. Just provide your Twitter screen name and how many tweets you want and that is it. The function returns a simplified result set because you rarely need all the stuff that the Twitter API throws your way. If you need everything then just put FALSE as the last argument.

Initial Title
PHP: Simple MyTweets Function

Initial Tags
twitter

Initial Language
PHP