// API LIB https://github.com/J7mbo/twitter-api-php

function get_last_tweet()
{
	/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
	$settings = array(
		'oauth_access_token' => "XXX",
		'oauth_access_token_secret' => "XXX",
		'consumer_key' => "XXX",
		'consumer_secret' => "XXX"
	);
	/** Perform a GET request and echo the response **/
	/** Note: Set the GET field BEFORE calling buildOauth(); **/
	$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
	$getfield = '?screen_name=USERNAME&count=1';
	$requestMethod = 'GET';
	$twitter = new TwitterAPIExchange($settings);
	$json = $twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest();
	$json = json_decode($json);

	// the rest of the script just parses through the data,
	// namely the date and time of tweet and any 'entities'
	// Borrowed from http://snipplr.com/view/54710/

	if (!empty($json)):
		foreach($json as $tweet):
			$datetime = $tweet->created_at;
			$date = date('M d, Y', strtotime($datetime));
			$time = date('g:ia', strtotime($datetime));
			$tweet_text = $tweet->text;

			// check if any entities exist and if so, replace then with hyperlinked versions

			if (!empty($tweet->entities->urls) || !empty($tweet->entities->hashtags) || !empty($tweet->entities->user_mentions))
			{
				foreach($tweet->entities->urls as $url)
				{
					$find = $url->url;
					$replace = '<a href="' . $find . '">' . $find . '</a>';
					$tweet_text = str_replace($find, $replace, $tweet_text);
				}

				foreach($tweet->entities->hashtags as $hashtag)
				{
					$find = '#' . $hashtag->text;
					$replace = '<a href="http://twitter.com/#!/search/%23' . $hashtag->text . '">' . $find . '</a>';
					$tweet_text = str_replace($find, $replace, $tweet_text);
				}

				foreach($tweet->entities->user_mentions as $user_mention)
				{
					$find = "@" . $user_mention->screen_name;
					$replace = '<a href="http://twitter.com/' . $user_mention->screen_name . '">' . $find . '</a>';
					$tweet_text = str_ireplace($find, $replace, $tweet_text);
				}
			}

		endforeach;
	endif;
	return $tweet_text;
}

echo get_last_tweet();