Connect your Twitter App using PhP for Twitter API v1.1
Friends,
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
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 "".$tweet->text."
"; }
- 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-
- Created At
- ID (Tweet ID)
- Text (User’s Tweet)
- Source (From where the tweet was generated)
- 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.