PHP - Twitter followers count

php-twitter-followers-count

With the twitter API you can retrieve info for specific user. All you will need is to specify a screen name.

function twitter_followers_count($screen_name){

    $data = file_get_contents("https://cdn.api.twitter.com/1/users/lookup.json?screen_name=" . $screen_name);
    $data = json_decode($data, true);
    return $data[0];
}

$twitter = twitter_followers_count("twitter");
print_r($twitter);
echo "Followers count: " . $twitter['followers_count'];

Output:

Array
(
    [name] => Twitter
    [friends_count] => 119
    [geo_enabled] => 1
    [id_str] => 783214
    [verified] => 1
    [id] => 783214
    [lang] => en
    [listed_count] => 76917
    [statuses_count] => 1578
    [utc_offset] => -28800
    [profile_background_color] => ACDED6
    [description] => Your official source for news, updates and tips from Twitter, Inc.
    [screen_name] => twitter
    [url] => http://blog.twitter.com/
    [followers_count] => 18357312
    [profile_link_color] => 038543
    [favourites_count] => 22
)

Followers count: 18357312

To retrieve info for more than one screen name, separate them by comma:

function twitter_followers_count($screen_name){

    $data = file_get_contents("https://cdn.api.twitter.com/1/users/lookup.json?screen_name=" . $screen_name);
    $data = json_decode($data, true);
    return $data;
}

$twitter = twitter_followers_count("twitter,php");
print_r($twitter);
- Posted by Eva to Php