MENU

Since the introduction of the Twitter API v1.1, every existing Twitter feed has had to be replaced or rewritten. This was because they now require oauth for authentication. The previous Twitter feeds worked anonymously via javascript, which can no longer be done with the new API. D’oh!

So we at Adao* decided we needed to write a custom Twitter feed that can easily be included where needed. This is a PHP script, so any pages that it is inserted in needs to be able to handle it.

API v1.1 Twitter Feed

First, you need to download the Twitter API Exchange and upload it to your site so the new snippet can refer to it. You can download this here.

Then, go to https://dev.twitter.com/apps/ and create a new application (please note you will need to be signed in to a Twitter account to do this). Fill out all the required fields and create the bad boy. you will need to specify which website this Twitter feed is going to be used on. This means that you will have to create a new app for every website you want the Twitter feed to be on.

Once created, you should be given an oauth access token, oauth token secret, consumer key, and a consumer secret. These are the special keys that you need to allow the widget to work.

Time for the snippet:

<?php
require_once(‘/YOUR_PATH_TO/TwitterAPIExchange.php’);

// Twitter Feed written by Adao* http://www.adaodesign.com
$url = ‘https://api.twitter.com/1.1/statuses/user_timeline.json’;
$getfield = ‘?screen_name=YOUR_TWITTER_NAME&count=1’;
$requestMethod = ‘GET’;
$settings = array(
‘oauth_access_token’ => “ENTER_OAUTH_ACCESS_TOKEN”,
‘oauth_access_token_secret’ => “ENTER_OAUTH_ACCESS_TOKEN_SECRET”,
‘consumer_key’ => “ENTER_CONSUMER_KEY”,
‘consumer_secret’ => “ENTER_CONSUMER_SECRET”
);
$twitter = new TwitterAPIExchange($settings);
$result = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$tweets = json_decode($result);
$output = ”;

if(!function_exists(‘links_to_hrefs’)){
function links_to_hrefs($input){
$reg_exUrl = “/(http|https|ftp|ftps)://[a-zA-Z0-9-.]+.[a-zA-Z]{2,3}(/S*)?/”;
// The Text you want to filter for urls
// Check if there is a url in the text
if(preg_match($reg_exUrl, $input, $url)) {
// make the urls hyper links
return preg_replace($reg_exUrl, ‘<a href=”‘ . $url[0] . ‘”>’ . $url[0] . ‘</a> ‘, $input);
} else {
// if no urls in the text just return the text
return $input;
}
}
}
foreach($tweets as $tweet){
$output .= ‘<p>’ . links_to_hrefs($tweet->text) . ‘</p> <a class=”retweet” href=”https://twitter.com/intent/retweet?tweet_id=’ . $tweet->id . ‘#retweet-intent” target=”_blank”>Retweet</a>’;
}

return $output;

?php>

Hopefully it’s pretty self explanatory, but you will need to change the bits in CAPITALS.

This code basically prints out the latest tweet in the format specified in $output. You can alter this to whatever you want, and then style it as required.

There is a section of code that deals with any links that are in the tweet, and automatically wraps it in a href tag so they become actual links. You can alter how many tweets are shown by changing the last number (count) in the line $getfield = ‘?screen_name=YOUR_TWITTER_NAME&count=1’;

As a highly trained team of web developers, Adao* are market leaders in providing any custom programming or website development. Feel free to contact us now to discuss your requirements.