Php Twitter Followers Count
The following PHP Twitter Followers Count script is using the new v1.1 Twitter API.
Overview
Recently, Twitter have changed their API and v1.0 is obselete. The new API requires OAuth. I’ll explain how to get your twitter followers count using PHP.
Create Application
First, you’ll need to get yourself a twitter dev account, if you haven’t done so yet.
Go here and register your app free.
TwitterAPIExchange
Download the TwitterAPIExchange library files created by J7mbo into your application.
you can git clone https://github.com/J7mbo/twitter-api-php
.
I recommend the git method as you can git pull later to recieve updated.
Get Followers Count
Insert your OATH and Consumer Access-Token & Access-Token secret the the following code. Also, change the $getfield screen name to yours.
Now combine everything to make it work
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "YOUR_OAUTH_ACCESS_TOKEN",
'oauth_access_token_secret' => "YOUR_OAUTH_ACCESS_TOKEN_SECRET",
'consumer_key' => "YOUR_CONSUMER_KEY",
'consumer_secret' => "YOUR_CONSUMER_SECRET"
);
$ta_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=replace_me;
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$follow_count=$twitter->setGetfield($getfield)
->buildOauth($ta_url, $requestMethod)
->performRequest();
$data = json_decode($follow_count, true);
$followers_count=$data[0]['user']['followers_count'];
echo $followers_count;
Notes
I’ve placed the entire twitter-api-php folder inside my project. you can extract the files also, but make sure to require_once() from the correct path.
require_once(‘twitter-api-php/TwitterAPIExchange.php’);
if you’ve placed it inside the folder, or
require_once(‘TwitterAPIExchange.php’); if you extracted the files to the same folder as your script.
More information
This article was based on the work of Jimbo and Meenakshi Sundaram R
Development Specialist, Artist and Activist
Personal Website