How To Use Twitter API To Display Your Twitter Updates

Updated: On March 5th 2013 Twitter is closing down the first version of the API and it will be fully replaced by version 1.1. Learn how to use the new API here http://www.paulund.co.uk/using-the-new-twitter-api-v1-1

Twitter Disc
It is common for blogs to display their twitter feed on the page which is mostly displayed in the sidebar of your wordpress blog.

Now there are many ready made wordpress plug-ins which will display your twitter feed and some have much more functionality. But do you really need more? From a twitter feed you just want your twitter feed.

So in this tutorial you will learn how you can get your twitter feed using the Twitter APIs and display these updates on your wordpress blog. For future posts I will also show you that you can then turn this functionality into your own wordpress plugin.

Plug In

Why Not Just Use A Ready Made Plugin

Displaying the twitter feed is simple functionality all you have to do is get your updates and display them on the page, no other functionality is needed...therefore no need to download a plugin which will add some extras.

You get full control of the mark-up you use so you can style your feed exactly how you want and display exactly what data you want.

Take full advantage of the Twitter API. Using the Twitter API you can get all the data for any twitter account just by supplying the username, no verification is needed. Also using the Twitter API you can get more data than the user timeline you can also get lists to display on the page.

Getting Updates Using Twitter API

To start with I will just show you how to use the Twitter API to get the data that you want, this is so if you want to just take this code and put it into your application you can. But I will also show how you can take this code and make your own wordpress plugin which will add a widget to display your twitter feed.

To access the Twitter API you can use a number different methods this is because you can access the data in a number of different formats...RSS, XML and JSON.

In this example I will be using JSON, this is because I prefer working with JSON rather than XML as it is a much smaller markup so can handle much larger chunks of data.

JSON stands for JavaScript Object Notation which is a lightweight data-interchange format, which makes it perfect to use for APIs as it is a better format to use for data exchange.

What We Need To Send To Twitter API

There is only 2 bits of data we need to send therefore to twitter that is the username and the number of tweets.

$username = ‘paulund_’;
$num = ‘10’;

Connect To Network

Access Twitter Data Via The API

To get the data out of twitter we need to do two things, send the request to the API, then process the response into a array of JSON data.

$username = 'paulund_';
$num = '10';

// Get the data from Twitter JSON API
$json = wp_remote_get("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=$username&count=$num");
	 
// Decode JSON into array
$data = json_decode($json['body'], true);

This will first use the wordpress function wp_remote_get which will get the response from a URL using HTTP GET and return the data in an array.

We need to send the screen_name and the count parameters to twitter so it knows which account to search on and how many tweets to return.

Using this way of getting the data means we can easily change the URL to get different data from twitter. For example you can use this same functionality to get list tweets, by using this Twitter API to get lists.

http://api.twitter.com/1/$username/lists/LIST_NAME/statuses.json?per_page=$num

We then use this array and store the data in a variable called $json, we can now decode the json into an array so we can process the twitter feed.

The json_decode function will take 2 parameters to turn it into an array. The first parameter is a string of JSON and the second if true will return the data as a associative array.

Now we have the array of twitter data we can simply loop through the response and display the twitter results.

Slideshow

Display The Tweets

echo '<ul class="twitter">';

foreach($data as $tweets){
	$text = $tweets['text'];
	$date = $tweets['created_at'];
        $time = strtotime($date);

	//Make time human readable ie. 6 hours ago
	$h_time = sprintf( __('%s ago'), human_time_diff( $time ) );

	echo '<li>'.$text . ' '. $h_time . '</li>'; 
}

echo '</ul>';

This code will loop through all the data from twitter and create a list with list items of your twitter updates. It will get certain bits of data from the twitter feed such as the text which is the text in the tweet and the created_at which is the date it was tweeted. You can then use the human_time_diff function to get the amount of time ago this was so you can display the twitter 6 hours ago timestamp next to your tweets.

It’s that simple you can now use the Twitter API to get your status updates and displays them on your page.

Code

Full PHP Code To Display Twitter Updates

//Set parameter for query string
$username = 'paulund_';
$num = '10';

// Get the data from Twitter JSON API
$json = wp_remote_get("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=$username&count=$num");
	 
// Decode JSON into array
$data = json_decode($json['body'], true);

//Create unordered list
echo '<ul class="twitter">';

//Loop through all twitter tweets and display the text with the time it was created
foreach($data as $tweets){

	$text = $tweets['text'];
	$date = $tweets['created_at'];

	//Make time human readable ie. 6 hours ago
	$h_time = sprintf( __('%s ago'), human_time_diff( $time ) );

	echo '<li>'.$text . ' '. $h_time . '</li>'; 
}

//Close list
echo '</ul>';

Conclusion

To take this further we can turn this code into a WordPress widget to make it easier to remove at a later date. Alternatively you can take the above example and code it directly into your sidebar.php file to display on the page.

Turn It Into A WordPress Widget

I am going to turn this code into a WordPress widget and will show you exactly how this is done in a future tutorial.

Here is the tutorial about Create WordPress Widget To Display Twitter Updates

Advertise here

Comment