10

How to get my followers count number with PHP.

I found this answer here: Twitter follower count number, but it is not working because API 1.0 is no longer active.

I have also tried with API 1.1 using this URL: https://api.twitter.com/1.1/users/lookup.json?screen_name=google but is is showing an error(Bad Authentication data).

Here is my code:

$data = json_decode(file_get_contents('http://api.twitter.com/1.1/users/lookup.json?screen_name=google'), true);
echo $data[0]['followers_count'];
2
  • 2
    You need to use an oauth connection now with v1.1
    – Novocaine
    Jul 1, 2013 at 16:25
  • And how can I use it?
    – Idrizi.A
    Jul 1, 2013 at 16:28

2 Answers 2

24

If do it without auth (replace 'stackoverflow' with your user)

$.ajax({
    url: "https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names=stackoverflow"
    dataType : 'jsonp',
    crossDomain : true
}).done(function(data) {
    console.log(data[0]['followers_count']);
});

with php

$tw_username = 'stackoverflow'; 
$data = file_get_contents('https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names='.$tw_username); 
$parsed =  json_decode($data,true);
$tw_followers =  $parsed[0]['followers_count'];
2
  • 2
    This answer is best if you only need simple follower count/data with no auth! Excellent! Edit: however unsupported by twitter - subject to change without notice.
    – mkabatek
    Jan 17, 2016 at 19:36
  • You can also use a user id to get info, e.g. https://cdn.syndication.twimg.com/widgets/followbutton/info.json?user_ids=128700677
    – Finesse
    May 15, 2018 at 2:13
18

Twitter API 1.0 is deprecated and is no longer active. With the REST 1.1 API, you need oAuth authentication to retrieve data from Twitter.

Use this instead:

<?php 
require_once('TwitterAPIExchange.php'); //get it from https://github.com/J7mbo/twitter-api-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;
?>

Parsing the XML might be easier in some cases.

Here's a solution (tested):

<?php 
$xml = new SimpleXMLElement(urlencode(strip_tags('https://twitter.com/users/google.xml')), null, true);
echo "Follower count: ".$xml->followers_count;
?>

Hope this helps!

4
  • small typo, missing a single quote after replace me
    – Novocaine
    Jul 1, 2013 at 16:32
  • Thank you, but I found this twitter.com/users/show/google . Is there any short PHP code to get just the followers number?
    – Idrizi.A
    Jul 1, 2013 at 16:45
  • It is showing these errors: jsfiddle.net/enve/XYcTX (I can't write here in comment because it is too long)
    – Idrizi.A
    Jul 1, 2013 at 17:39
  • Hi, above code is not working.It's showing nothing when echo from json.Any idea? Nov 1, 2013 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.