Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a count of followers from Twitter API and trendline

Tags:

twitter

I am in the process of writing some reports for the number of followers over time for Twitter, however after substantial searches and trial and error, I have not being able to get the number of followers over time - particularly past number of followers.

I know there is an API to get the individual userIds for the followers, but thats an overkill for what I need and I would have to call it everyday. Ideally it would be great if I can pass a date and it could return the number of followers.

Does anyone have any experience with this and what the API might be!

Thanks

like image 656
Jason Jong Avatar asked Nov 03 '10 07:11

Jason Jong


2 Answers

While there is no direct API to get the trendline, getting the followers count is fairly easy, access via the URL:

http://api.twitter.com/1/users/show.json?user_id=12345

The documentation has it all @ https://dev.twitter.com/docs/api/1/get/users/show

To get the trendline, seems like I will need to query it on a daily basis!

Updated to Twitter API v1.1

https://api.twitter.com/1.1/users/show.json?user_id=12345

Documentation at https://dev.twitter.com/docs/api/1.1/get/users/show

Updated 31-May-2018

The new API end point is at

https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-users-show
like image 181
Jason Jong Avatar answered Oct 26 '22 01:10

Jason Jong


Here is a simple PHP exemple using CURL, with no library involved, to get the followers_count of a selected profile (here @TwitterFrance) using v2 API and the bearer token (bearer token is some kind of simplified method to access public data through an OAuth 2.0 API)

$authorization = "Authorization: Bearer YOUREXTRALONGBEARERYOUREXTRALONGBEARERYOUREXTRALONGBEARERYOUREXTRALONGBEARERYOUREXTRALONGBEARERYOUREXTRALONGBEAR";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', $authorization));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, "https://api.twitter.com/2/users/by/username/TwitterFrance?user.fields=public_metrics");
$result = curl_exec($ch);
curl_close($ch);
if (is_string($result)) {
    echo (json_decode($result)->data->public_metrics->followers_count);
    die();
}
like image 26
Paul ALBERT Avatar answered Oct 26 '22 01:10

Paul ALBERT