Categories: BlogTechnology

Twitter API: Retrieve User Timeline & Group Threaded Tweets

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.

See the gist on github.

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.

See the gist on github.

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.

See the gist on github.

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.

See the gist on github.

Final Thoughts

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.

 

Sunil Boodram

Software Consultant, Web Designer, Writer, and an Activist.

Share
Published by
Sunil Boodram

Recent Posts

Twitter: Registering and Creating a new Application

Creating a new Twitter application requires several important steps. First we must register a new application,…

7 years ago

Google: Search Console Beta Rickrolled Me

Anyone familiar with the old search console knows that manually adding each URL or even…

7 years ago

Fighting to Protect the Aripo Savannas

On Monday March 26th, Fishermen and Friends of the Sea (FFOS) had their request for…

7 years ago

Diabetes Heatmap of Trinidad & Tobago

In my last blog post I shared some statistical information on diabetes in Trinidad &…

7 years ago

Diabetes Statistics for Trinidad & Tobago

I my last blog post I wrote about diabetes in Trinidad & Tobago and tried…

7 years ago

Diabetes affecting 148,822 Trinidadians

Diabetes is on the rise in Trinidad & Tobago and people aren’t taking it seriously…

7 years ago