geeky · javascript · php code

Twitter API 1.1 php get tweets script

I’ve been using a nice caching php script for getting my latest tweets. Recently twitter completely stopped support for API 1 and everyone must use API 1.1. API 1.1 requires authentication and therefore making it more complicated to use the original script.

I’ve modified the script with the help of the codebird library to do what it used to do.

Set up authentication for your application

  • To use the new getTweets.php, you must go to your twitter dev account and create a new application.
  • Fill in all of the required information.
  • Once created, you will be given the Consumer key and Consumer secret.
  • Click on the Create my access token button, you will be given the Access token and Access token secret.
  • You will need the four pieces of information to use the script.

Set up the script on your server

  • Download my github fork
  • Open the src/getTweets.php file
  • Fill in the CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET
  • Update the time between cache in the Time between cache variable if you wish. Default is 5 min.

To use the script

Once done, upload all files in the src folder to a folder on your server. You may get the latest tweets for the user via ajax such as:

// get latest tweet
$.ajax({
	url: "{PATH TO}/getTweets.php",
	type: "GET",
	data: { count : '2', user : 'dodozhang21' },
	success: function(data, textStatus, jqXHR){
		var html = '<ul>';
		for(var x in data) {
			var tweet = data[x];
			//console.log(tweet);
			html += '<li>';
			html += tweet.text;
			html += '<span>';
			html += tweet.created_at;
			html += '</span></li>';
		}
		html += '</ul>';
		$('#latestTweet').removeClass('loading');
		$('#latestTweet').html(html);
	},
	error: function (jqXHR, textStatus, errorThrown){
		//console.log('Error ' + jqXHR);
	}
});

Additional Information

One thought on “Twitter API 1.1 php get tweets script

  1. i setup the key credentials but when i try to receive the tweets from the ajax code i receive blank white page is this the format for ajax ? {code}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s