June 13, 2013 by Nitesh

Connect your Twitter App using PhP for Twitter API v1.1


By the time you are reading this article, you must be knowing that Twitter has retired its API v1 and have ensured all new requests are served using v1.1 of the API. As I mentioned in my previous post, 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 post to know who to do the same.
  • Connect to the app using a library in the language of your choice.

This post 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 advantage of implementing this using a server side language (C#, PhP) is 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 which 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.

Lets 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 below code in your php file
$twitter_un = "niteshluharuka";
$num_tweets = 15;
$consumerkey = "XXXXX";
$consumersecret = "XXXXXX";
$accesstoken = "XXXXXX";
$accesstokensecret = "XXXXXXXX";
$connection = new TwitterOAuth($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
$tweets = $connection->get("".$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. Below is a sample output –

Understanding the code –

  • Very first, we include the core library file from the twitteroauth library. This file holds all the required functions to make connection to your app, 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 library and required credentials.
  • Once connection is successful, we try getting the data from 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 way.
  • Few of the metadata returned by the code for each tweet is as below-
    1. Created At
    2. ID (Tweet ID)
    3. Text (User’s Tweet)
    4. Source (From where the tweet was generated)
    5. Complete User Details

Hope you like this post and in case you face any issues while implementing the oAuth library, contact me via comments or via social media.

#How To?#oAuth#PhP#Twitter#Twitter API#Twitter APIv1.1
  • Vlad

    Hola que tal,

    Muy buen tutorial, mi pregunta es si se puede dar más formato a la saldia de los tweets, quizá en formato XML como si fuera un Rss, para que no se vea el puro texto simple. Saludos

    • Gracias. Usted puede utilizar el mismo código para generar un canal RSS de wirting un archivo XML.

  • Michael

    After 8+ hours looking for a SINGLE example of implementing v1.1 of the Twitter API – this one finally does. Thank you! I have no idea why the other 5 or 6 examples I tried didn’t work but this one – and the instructions – were perfect!

    • Nitesh Luharuka

      Glad Michael it helped you!

  • Subbu

    Thanks Nitesh.. it was very helpful

Support us!

If you like this site please help and make click on any of these buttons!

Powered by WordPress Popup