in PHP

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
![Twitter Disc]( "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]( "List Of Wordpress Plugins"). ![Plug In]( "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. ![]( "folder_updates")## 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]( "List Of Wordpress Plugins") 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. ![]( "1311886475_Forward")## 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]( "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("$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. ```$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]( "slideshow")## Display The Tweets ``` echo ''; ``` 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]( "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("$username&count=$num"); // Decode JSON into array $data = json_decode($json['body'], true); ``` //Create unordered list echo ''; //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 '- '.$text . ' '. $h\_time . ' '; } //Close list echo ' '; ## 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](

Upgrade to access all content on Paulund

Members unlock all tutorials and snippets

Access to all downloadable content

Access to code examples before others

Sign Up Now

Already a member? Login here

Subscribe To The Weekly Newsletter

Get weekly updates to your email