New Twitter API

twitter bird
Now although this sounds bad news for developers, there are many benefits to switching over to the new API and it is certainly going to improve things in the future.
Just a few of the benefits include:
• Improved rate limits – now up to 180 instances in 15 minutes
• Required authentication meaning less chance of system abuse
• A more in-depth and reliable feed service via JSON.
However, the new API does require a lot more technical expertise to integrate, and luckily for us, we have a fantastic team of developers who ‘cracked the code’ in no time! So what’s the secret? Just a simple piece of PHP and CURL, you can be up in no time at all with a new Twitter feed, and since we are feeling generous, we thought we would share this with you all.
We’ve commented the code for when you need to change, and for those more advanced users of PHP, feel free chop and change according to your server needs. Just remember to gather all your API and oAuth keys before you start, by visiting https://dev.twitter.com/apps/new
$value){
$r[] = “$key=” . rawurlencode($value);
}
return $method.”&” . rawurlencode($baseURI) . ‘&’ . rawurlencode(implode(‘&’, $r));
}
function buildAuthorizationHeader($oauth) {
$r = ‘Authorization: OAuth ‘;
$values = array();
foreach($oauth as $key=>$value)
$values[] = “$key=”” . rawurlencode($value) . “””;
$r .= implode(‘, ‘, $values);
return $r;
}
function returnTweet(){
This is where you put the application details (you will need to get up these in the client’s Twitter developer area)
$oauth_access_token = “545945350-72IRnJdtneZhqt5LmPBvl7F6sWbvsWFJnnmgRFBd”;
$oauth_access_token_secret = “3bJ0Jzu7QmiSTueOHD31HBqnjGq9zY4at8W86hopqVk”;
$consumer_key = “bZXo7eoRLryzOCAXGJSg1A”;
$consumer_secret = “k4ZJmPpNS8TnG0LNLAz7fOt3z5y6UQEEnVBLf3bM3Kg”;
$twitter_timeline = “user_timeline”;
Pop in what you want here; so the user handle and the number of tweets in this case
$request = array(
‘screen_name’ => ‘vickywclicky’,
‘count’ => ‘1’
);
No need to change this, keep it as is – this builds the authentication
$oauth = array(
‘oauth_consumer_key’ => $consumer_key,
‘oauth_nonce’ => time(),
‘oauth_signature_method’ => ‘HMAC-SHA1’,
‘oauth_token’ => $oauth_access_token,
‘oauth_timestamp’ => time(),
‘oauth_version’ => ‘1.0’
);
$oauth = array_merge($oauth, $request);
$base_info = buildBaseString(“https://api.twitter.com/1.1/statuses/$twitter_timeline.json”, ‘GET’, $oauth);
$composite_key = rawurlencode($consumer_secret) . ‘&’ . rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac(‘sha1’, $base_info, $composite_key, true));
$oauth[‘oauth_signature’] = $oauth_signature;
$header = array(buildAuthorizationHeader($oauth), ‘Expect:’);
$options = array( CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => false,
CURLOPT_URL => “https://api.twitter.com/1.1/statuses/$twitter_timeline.json?”. http_build_query($request),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
return json_decode($json);
}
This is the loop you will need. You assign the function results to a variable then loop it with a for loop. if you look at http://dev.clickymedia.co.uk/twitter/ you can see the full options available
function relativedate($secs) {
$second = 1;
$minute = 60;
$hour = 60*60;
$day = 60*60*24;
$week = 60*60*24*7;
$month = 60*60*24*7*30;
$year = 60*60*24*7*30*365;
if ($secs <= 0) { $output = "now"; } elseif ($secs > $second && $secs < $minute) { $output = round($secs/$second)." second"; } elseif ($secs >= $minute && $secs < $hour) { $output = round($secs/$minute)." minute"; } elseif ($secs >= $hour && $secs < $day) { $output = round($secs/$hour)." hour"; } elseif ($secs >= $day && $secs < $week) { $output = round($secs/$day)." day"; } elseif ($secs >= $week && $secs < $month) { $output = round($secs/$week)." week"; } elseif ($secs >= $month && $secs < $year) { $output = round($secs/$month)." month"; } elseif ($secs >= $year && $secs < $year*10) { $output = round($secs/$year)." year"; } else { $output = " more than a decade ago"; } if ($output <> “now”){
$output = (substr($output,0,2)<>“1 “) ? $output.”s” : $output;
}
return $output;
}
$getweets = returnTweet();
foreach ($getweets as $tweet) {
echo $tweet->text .”n”;
echo ” was tweeted “. relativedate(time()-strtotime($tweet->created_at)) .” ago n”;
}
?>
If you are looking to integrate social media into your website, or need help with the code above, then please feel free to contact us today.

Written by Oli Yeates

CEO/Founder

Follow Oli on Twitter