Category Archives: PHP

Getting Twitter TL Using API1.1 And Twitteroauth

This will get latest tweets from timeline and display it using twitter api v1.1 and twitteroauth library. For more read visit webdevdoor.

About Twitter API v1.1: API stands for application programming interface. It provides specifications on how applications can communicate with each other. For example text on notepad can be copied to and from word. With an excellent use of API many application can interact with yours thus make it more popular. Popular example is Twitter. For more info on API read article from makeuseof.

About Twitteroauthits a PHP library for working with Twitter’s OAuth API. you can get it here from Github

STEP 1: Create A New Application

  1. First you need to create a new application from Twitter Developer Site at https://dev.twitter.com/apps. Sign in and click create new application button.
  2. You will be directed to a page asking for your application details. fill it, click agree on the rules and enter the CAPTCHA.
  3. Now your application have been created.
  4. On details tab click create access token button to generate your access token.
  5. Now you will get all the information needed for OAuth; consumer key, consumer secret, access token, and access token secret.
    Untitled

STEP 2: Getting Twitter Feed

  1. Download Twitteroauth library by abraham at https://github.com/abraham/twitteroauth.
  2. Create new php file, example.php and paste this code.
    [sourcecode language=”python” wraplines=”false” collapse=”false”]
    <?php
    session_start();
    require_once("twitteroauth.php");//Path to twitteroauth library

    //change here
    $twitteruser="twitterusername";
    $notweets= 10;
    $consumerkey= "12345";
    $consumersecret= "123456789";
    $accesstoken= "123456789";
    $accesstokensecret= "12345";

    function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret){
    $connection= newTwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
    return $connection;
    }

    $connection= getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);

    $tweets= $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);

    echo json_encode($tweets);
    ?>

    [/sourcecode]

  3. Replace all the tokens with the one that you have created from your Twitter application.
  4. Add the file with twitteroauth.php from twitteroauth folder that you have downloaded in your server.
  5. You should get all the feed displayed in JSON format.