How to Get a Suitable Container List of Your Tweets

Programming
Renovated API 1.1

Recently, Twitter finally moved to the new version of its API - 1.1. As a result, there have been many changes from previous version.

Now, you can not search without authentication. Also restrictions on the number of connections per minute are imposed, such a method «search» 180 connections is limited to 15 minutes. As a transport protocol is available only JSON, XML is no longer supported.

In this post, I would like to describe how to get the user's tweets and make an online Twitter Feed, using the Twitter API 1.1

1. To begin, we need to have access to the Twitter account from which you would like to receive tweets.

- Go to page developers - https://dev.twitter.com and click under this Account.

- Go to the page «My applications» and click "Create new application"

- Fill in the appropriate fields and create an application.

- Then you get into the application page (application)

- Then go to the tab "OAuth tool", where we will need data from the fields: Consumer key, Consumer secret, Access token, Access token secret.

Ukietech blog about Twitter API

2. Now, with this information we can start connecting Tweets to the website :

- For this use, the PHP library "twitteroauth", which can be downloaded at https://github.com/abraham/twitteroauth

- Create a file tweets.php with the following code:

require_once (“twitteroauth.php");
$tweets_count = 25;
$consumerkey = your consumerkey;
$consumersecret = your consumersecret;
$accesstoken = your accesstoken;
$accesstokensecret = your accesstokensecret;
$connection
= $this->getConnectionWithAccessToken($consumerkey, 
$consumersecret,$accesstoken, $accesstokensecret);
$tweets =
$connection->get("https://api.twitter.com/1.1
/statuses/user_timeline.json?count="
. $tweets_count);
$tweets = json_encode($tweets);
echo $tweets;
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;    }      

   - The file where we want to display tweets, can be grouped via Ajax contact file tweets.php file and get them in JSON format, the code will be as follows:

<div id="tweet-container"></div>
<script>
$.ajax({
    dataType:"json",
    url:'tweets.php',
    success:function(data){
      container = $('#tweet-container');               
      $.each(data, function( key, value ) {
        var str = '<div class="tweet">\
        
<div class="avatar"><a href="http://twitter.com/
      '+value.from_user+'" target="_blank">\
         
<img src="'+value.user.profile_image_url+'"/></a></div>\

<div class="user"><a href="http://twitter.com/'+value.user. screen_name+'"target="_blank"> '+value.user.screen_name+'</a></div>\           <div class="time">'+value.created_at+'</div>\           <div class="txt">'+value.text+'</div>\                  </div>';           container.append(str);                             });     } }); </script>

Now we have a suitable container list of our tweets :)

See also: Zen Coding – the Method of Quick Coding

Comments