PHP integration with Twitter API 1.1

I got caught out by the recent disabling of the Twitter API 1.0, like many others it seems. I knew it was coming. However I’d held off in the hope that Twitter would see sense on these changes, but apparently not.

It was time to add my token few hours into the vast amount of web-development hours wasted as a result of the Twitter API changes.

I have a simple setup on a few websites, just to retrieve the last few tweets from a timeline and show them on part of my webpage. This is publicly available information, so a typical configuration has been to use an Ajax approach with jQuery to get the timeline url for the user, and then parsing the returned json in javascript, replacing a placeholder on the webpage with the now formatted tweets.

The Twitter 1.1 API includes a need to authenticate using OAuth, for all calls. Also there seems to be a general push towards using a server-side method of connection, which probably significantly reduces the number of connections they receive, in place of the client-side javascript approaches.

I decided to use a server-side PHP approach, as there is this a Twitter OAuth integration library on GitHub, and I already have similar server-side integration with FormSpring on one of my sites.

First you have to logon to the Twitter developers centre to register an application and to generate keys. These are then used to call OAuthTwitter to create an authenticated connection. This can then be used to call the desired URL. This all went very smoothly.

function getTweets($twitteruser) { 
	$notweets = 20;
	$consumerkey = "insertkey";
	$consumersecret = "insertsecret";
	$accesstoken = "inserttoken";
	$accesstokensecret = "inserttokensecret";
	function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
	  $connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
	  return $connection;
	$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
	$tweets = $connection->get("".$twitteruser."&count=".$notweets);
	return ($tweets);

At first I had a few problems parsing the output: as it was a json url I assumed that json was being returned. After a bit of debugging I realised that the result is being handled as an associative array. I could now loop through and output a number of tweets.

$tweets = getTweets($twitteruser);
foreach ($tweets as $line){
    $status = $line->text;
    $tweetTime =  $line->created_at;
    $tweetId = $line->id_str;
    $outputTweet = '<li>'.$status.'</span> <a style="font-size:85%" href="'.$twitteruser.'/statuses/'.$tweetId.'">'. $tweetTime .'</a></li>';
    echo $outputTweet;    

The raw output highlighted a few things to change. I decided I didn’t want to show replies or retweets and updated the Twitter url to add parameters to remove these.

$tweets = $connection->get("".$twitteruser."&count=".$notweets."&include_rts=0&exclude_replies=true");

The time was being shown as an absolute time rather than a relative ‘1 hour ago’ style. I considered writing a function to reformat this, but a quick search found a nice version already written by Zachary Jones, so in the spirit of reuse I adopted that instead. Similarly I noticed that hashtags, usernames and links were not formatted as links. Again a quick search found a function by Mardix that appears to handle this, so again I adopted it rather than writing my own from scratch.

So I now have working Twitter widgets again. Which is nice. But somewhat disappointing that the change was even needed.

1 Comment

  1. Hi,

    until now I have reach how to show all the tweets I’ve published using mi twitter account using this code:


    But I would like to know if is there any way to show all the tweets that contains my user name @twitteruser and that have published other users.

    Thanks a lot

Leave a Reply

Your email address will not be published. Required fields are marked *