Our Blog

Recent Posts

Categories

27Jun

PHP tip: How to get number of followers from your Twitter account

Here is a simple solution to get the number of followers from your twitter account:
    $twitterUsername = 'yourusername';
    $countTwitterFollowers = file_get_contents("http://twitter.com/statuses/user_timeline/" . $twitterUsername . ".json?count=1");
    $countTwitterFollowers = json_decode($countTwitterFollowers);   
    echo number_format($countTwitterFollowers[0]->user->followers_count);
Common practice today is to disable allow_url_fopen on your web server which will result in the following error:
    file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration
To solve this problem, allow_url_fopen should be ON in PHP configuration (php.ini).
Categorised under: PHP, Twitter