In this post we will look into accessing Twitter REST API in PHP. This can be useful if you need to post Tweets from your PHP application or anaylze, search Tweets. In the following examples we will use the twitter-api-php PHP wrapper for Twitter v1.1 API. Although there are a few wrappers around, this one I like for its simplicity.
If you use composer, here’s what you need to add to your composer.json file to have TwitterAPIExchange.php (Twitter PHP wrapper) automatically imported into your vendor’s folder:
You’ll then need to run ‘composer install’.
If you don’t use composer, just download the zip from git and include TwitterAPIExchange.php in your application path.
Registering your Twitter app
Before writing any code, you will first need to create a Twitter app and register it onhttps://apps.twitter.com/. You will be greeted with the following screen. (The following screenshot shows you an existing app. If this is your first app than the page will be blank).
Click on the ‘Create New App’ button and enter the required details in the fields provided. Leave the ‘Callback URL’ field blank for now. Once the app is created, click on the app name and note down the various access and security tokens, we will be needing these later. Change your Access Level to ‘Read and Write’. ‘Read Only’ access does not allow you to update, add or delete Tweets. If your purpose is to only read tweet data, it is safer to set the Access Level to ‘Read Only’.
Note that you will need to regenerate the access tokens if you change the Access Levels anytime and modify the same in your PHP code.
Accessing the Twitter API from PHP
Now that you have created and registered a Twitter app, we can now use PHP to acccess the API. First include the ‘TwitterAPIExchange.php’ class and set various security tokens collected earlier.
For this example we will be using the ‘https://api.twitter.com/1.1/statuses/user_timeline.json‘ resource url. This returns a collection of the most recent Tweets posted by the user indicated by the
screen_name or user_id parameters. This particular method is one of the dozens of various methods available to access and modify Twitter data; others can be found here.
A complete GET request method is shown below. Here we are requesting the recent 3 tweets for the user ‘johndoe123’.
Once we specify the request methods and fields, we can call Twitter.
The complete code is shown below.
Executing the above code will return the recent 3 tweets for the given user in JSON format. You can specify the number of tweets to return using the ‘count parameter. The maximum that can be returned is 200.
Note that all GET fields for this method are optional. Not specifying any GET parameters will return 20 recent tweets for the current user, i.e the user with the given access tokens. You can query without the GET parameters as shown below.
Also, the response is returned in JSON, so will need to decode the JSON first before working further.
The tweet is stored in the ‘text’ field of the response, so we can enumerate all returned tweets with the following. Various other fields can be accessed in similiar manner.
Posting a new Tweet
Posting a new Tweet can be done with the following code, which uses a POST method instead of a GET. The initial code remains the same.
There are additoinal parameter you can use, the details for which are given here.
Searching for Tweets
Searching for Tweets based on a query is as easy as the above examples. Here in the following example we query for the ‘laravel’ keyword and ask o return 10 statuses.
API rate limits
One important point to consider when creating your app is of rate limiting. Twitter limits the rate at which you query using their API. For example search will be rate limited at 180 queries per 15 minute window. More information can be foundhere.
If the above examples do not work you will have to make a small change in ‘TwitterAPIExchange.php’. The curl processing part (around line 192) requires the line ‘CURLOPT_SSL_VERIFYPEER => false,’ to be added. So the curl part in the file should read the following.