A client recently requested that their latest tweet be shown on the homepage. After playing around with the JavaScript API I learnt it wasn’t showing in IE9 due to incorrect headers being sent from Twitter, so I decided to create a simple PHP class to handle the process.

The idea was simple: pull an account’s latest tweets from Twitter and have a few options such as profile linking.
The demo includes some basic styling which would ideally be moved to a seperate class.

I will probably expand on this at a later date so it’s just a basic PHP class at the moment, but should be fairly easy to add to or edit.

» Demo
» View Source


Using the script only requires a few lines of code…

$my_tweets = new pull_tweets();

echo $my_tweets->tweets('mradamdavies', 5, true, true, true, true);

The first line will instantiate the pull_tweets class.
The second line will output the code using the options given, which are:

mradamdavies = Twitter account name
5 = number of tweets to show.
The 4 true/false flags toggle the following:
Show @profile links, Show #tag links, Show twitter avatar, Show twitter profile link.

The code

The full class is as follows.

	PHP Twitter Feed Importer

class pull_tweets { // Create a basic class

var $twitter_handle; 	// Twitter username
var $tweet_limit;		// Max tweets to return
var $tweet_links;		// Show @profile links
var $tweet_tags;		// Show #tag links
var $tweet_avatar;		// Show twitter avatar
var $tweet_profile;		// Show twitter profile link

var $hard_max;			// Real max tweets (Used to filter out @replies)
var $i;					// Tweet counter

function show_tweet_links($tweet_links_output, $tweet_links) { // Find and replace @replies with links
		if($tweet_links == true) {
			$find  = '/@([a-zA-Z]+)/';
			$replace  = '<a class="hash_link" href="http://twitter.com/'.strtolower('1').'" rel="nofollow">@1</a>';
			$tweet_links_text = preg_replace($find,$replace,$tweet_links_output);
			return $tweet_links_text;
		else { return $tweet_links_output; }

function show_hash_tags($tweet_hashtags_output, $tweet_tags) { // Find and replace #tags with links
	if($tweet_tags == true) {
		$find_hash_tag  = '/#([a-zA-Z]+)/';
		$replace_hash_tag  = '<a class="hash_link" href="http://twitter.com/search?q=%23'.strtolower('1').'" rel="nofollow">#1</a>';
	else { return $tweet_hashtags_output; }

function show_twitter_avatar($tweet_avatar_output, $tweet_avatar) { // Show avatar
	if($tweet_avatar == true) {
		return '<img src="'.$tweet_avatar_output.'" alt="" />';
	else { return false; }

function show_twitter_profile_link($tweet_profile_output, $tweet_profile) { // Insert profile link
	if($tweet_profile == true) {
		return '<a class="profile_link" href="http://twitter.com/'.$tweet_profile_output.'">@'.$tweet_profile_output.'</a>';
	else { return false; }

// Create a basic function to pull latest tweets
function tweets($twitter_handle, $tweet_limit, $tweet_links, $tweet_tags, $tweet_avatar, $tweet_profile) {

		/* Store Tweets in a JSON object */
		$tweet_feed =	json_decode(file_get_contents('http://api.twitter.com/1/statuses/user_timeline.json?screen_name='.

		$i = 0; // Start the tweet limit counter
		foreach ($tweet_feed as $tweet) { // Loop through the tweets

			if ($tweet->in_reply_to_user_id == '' && $i != $tweet_limit) { // Don't show direct @replies
				++$i; // Increase tweet counter

	// Check true/false flags and return output accoringly
	$tweet_text = $tweet->text;
	$tweet_text = pull_tweets::show_tweet_links($tweet_text,$tweet_links); // Show @reply links?
	$tweet_text = pull_tweets::show_hash_tags($tweet_text,$tweet_tags); // Show #tag links?
	$twitter_avatar =
pull_tweets::show_twitter_avatar($tweet->user->profile_image_url,$tweet_avatar); // Show avatar?
	$twitter_profile =
	pull_tweets::show_twitter_profile_link($twitter_handle,$tweet_profile); // Show main profile link?

	// Echo our tweet contents
	echo '
	<div class="tweet_bg">',$twitter_avatar,'
	<p>'.$tweet_text.'<br />'.$twitter_profile.'</p>


		} // end of foreach

	} // end of tweets function

} // end of pull_tweets class

The JSON object returns a lot of data, only some of which is used in this example.
Other notable resources include, but is not limited to:

$tweet->in_reply_to_user_id; // Is a reply?
$tweet->user->name; // Real name
$tweet->user->screen_name; // Twitter username
$tweet->user->description; // Profile "Bio" text
$tweet->user->url; // Profile "Web" URL
$tweet->user->friends_count; // Friend count
$tweet->user->followers_count; // Follower count
$tweet->created_at; // Tweet date

If you wish to view the entire output, just add the following under ++$i;

echo '<pre>';
echo '</pre>';

I made a small change as I forgot to use the hard_max variable.

Please let me know if you have any issues or suggestions as I will be working on this a little more.