In a previous article I wrote about creating and registering a new twitter application. Creating applications on Twitter provides us with keys and tokens, that we may use to create requests in which we retrieve or post data to Twitter accounts, using the Twitter API.
Aggregating Tweets from various public sources is an excellent way to gather related content and use it in applications. Another common task in interacting with the Twitter API is the ability to read Tweets from a user timeline. Retrieved Tweets are used in many different ways, embedded in web pages, shared in email newsletters, cross-posted to other social media accounts…etc.
Twitter Application-only authentication
With Application-only authentication, you can retrieve tweets from a user timeline, without defining a user context in your application. This means that you don’t have to specify an authenticated user account in your application, in which to make requests. However, since this is read-only, you cannot use certain features, such as posting tweets.
Grouping Threaded Twitter Tweets
One issue with retrieving tweets from a timeline is threaded Tweets or threaded replies. These are a series of Tweets grouped together to create a threaded response. It would make sense to collect threaded Tweets and group them together to create a complete story. However, this requires several extra steps because the format of returned Tweet data does not specify an identifier for each threaded Tweet. Instead, threaded Tweets contain a field called “in_reply_to_status_id,” where the most recent Tweet in a threaded Tweet contains the Tweet ID of the previous Tweet, until the previous Tweet has a value of null.
Retrieve Tweets From a User Timeline
Below you will find a breakdown of code which create an application-only request to retrieve Tweets from a user timeline. Threaded Tweets are grouped together in an array and the Tweet ID is used as the index. Similarly, single Tweets are assigned to individual arrays where their Tweet ID is also assigned as the index.
Three functions are supplied with ample comments for each line of code. Anyone with an intermediate knowledge of PHP should be able to follow along easily. I will post the entire file at the end of this article.
Get Twitter Bearer Token
When making application-only requests, the first step is to retrieve a “bearer token.” To retrieve a “bearer token” you must supply your “consumer key” and “consumer secret”, which is supplied to you in the Twitter application you created or will create. Replace line 18 and 19 with your “key” and “secret”. You shouldn’t need to change anything else.
Request Twitter Tweets
After you retrieve your “bearer token” you can pass it into the api_twitter_request() function.
The function is currently set to retrieve the timeline of the Trinidad & Tobago Police Service (TTPoliceService.) You can change this to any public Twitter account by supplying the accounts screen name.
If you wish to receive more Tweets, set the “count” value higher. If you wish to include the “user object” you can simply remove the “trim_user=1” parameter. I would recommend leaving the “tweet_mode=extended” as is, since changing this parameter will trim Tweets and retrieve only a snippet of a Tweet.
Get & Format Twitter Tweets
After you receive data from a Tweet request, you may optionally pass it into the “get_tweets()” function.
The “get_tweets()” function loops over the Tweet data and builds a multidimensional array which groups threaded Tweets and Tweet replies together. Single Tweets are also separated into their own individual array.
Full Code Sample
This is the complete code sample to retrieve a user timeline and combine threaded tweets. At the very end of the code sample you will find a formatted example of what your complete request would look like, if you used the script.
If you wish to include other Tweet data in your output, simply add the fields you want in the “get_tweets()” function, inside the “foreach()” loop.
I hope this helpful. If you’re having issues, have any questions, find any bugs, or know a better way, please don’t hesitate to leave a comment.