Connect Your Twitter App Using PHP For Twitter API V1.1

By the time you read this article, you must know that Twitter has retired its API v1 and now ensure that all new requests are served using v1.1 of the API. As I mentioned in my previous article, we now need to authenticate users before trying to retrieve the user's timeline. For this, we need to perform 2 steps. They are:

  • Create a Twitter App for authenticating using Twitter API v1.1. Please read this article to know how to do that.
  • Connect to the app using a library in the language of your choice.

This article describes how to connect your Twitter app to get the user's Tweets via PHP.

We will be using the twitteroauth library written for PHP to implement this. You can get the library from github here. One of the biggest advantages of implementing this using a server side language (C#, PHP) is that you do not expose the secret keys of your app to the end users. The app is called from the server end using secure parameters and then only the data in JSON format is returned back to the end user that you can opt to show in your own specific design.

Prerequisites for running the code:

  • PHP/WAMP/LAMP installed and configured
  • cURL enabled on your server or in your WAMP/LAMP installation
  • Details for the Twitter App you created

Let's write some code now

  • Download the twitteroauth library files from here
  • Unzip the files on your Computer
  • Create a new PHP file in the folder and name it as getTweets.php
  • Copy and paste the following code in your PHP file:
     

    require_once("twitteroauth/twitteroauth/twitteroauth.php");

    $twitter_un = "niteshluharuka";

    $num_tweets = 15;

    $consumerkey = "XXXXX";

    $consumersecret = "XXXXXX";

    $accesstoken = "XXXXXX";

    $accesstokensecret = "XXXXXXXX";

    $connection = new TwitterOAuth($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);

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

    foreach($tweets as $tweet) {

        echo "<p>".$tweet->text."</p>";

    }
     

  • Replace the Twitter username with yours

  • Enter the number of Tweets you want to retrieve

  • Replace the Consumerkey, Consumer Secret, Access Token and Access Token Secret with the ones from your app.

  • Save the file and run it on your browser.

You will see the number of Tweets requested by the file on your browser. The following is a sample output.

Understanding the code

  • We first include the core library file from the twitteroauth library. This file holds all the required functions to make the connection to your app and authorize your web page to retrieve data from the timeline.
  • We set up the required variables
  • We make a connection using the TwitteroAuth() class defined in the library and the required credentials.
  • Once the connection is successful, we try getting the data from the user's timeline
  • The data returned by the code is an array and we can then iterate over the array to display the Tweets in our desired manner.
  • A few of the metadata returned by the code for each Tweet are the following:
    1. Created At
    2. ID (Tweet ID)
    3. Text (User's Tweet)
    4. Source (from where the tweet was generated)
    5. Complete User Details

I hope you like this article and in case you encounter any issues implementing the oAuth library, contact me via comments or via social media.