422
422

Reputation: 5770

Show twitter followers in Plain Text

I am using the following php code to display Facebook Fans. Was hoping to do something similar for Twitter.

Any help Appreciated.

<?php
function fbfan() {
$pageID = 'facebookID';
$info = json_decode(file_get_contents('http://graph.facebook.com/' . $pageID));
echo $info->likes;
}
?>

Using :

<p><?php fbfan(); ?> Facebook Fans</p>

The above works great meaning we can style to our hearts content, plus its lightweight.

Is there anything remotely similar for Twitter ? To echo number of followers.

Upvotes: 1

Views: 1853

Answers (3)

Prize
Prize

Reputation: 1

This function will work, just input your username.

<?php

function getTwitterFollowers($screen_name) {
  $url = "http://api.twitter.com/1/statuses/user_timeline.json?count=1&screen_name=" . $screen_name;
  $data = json_decode(file_get_contents($url), true);
  return $data[0]["user"]["followers_count"];
}

?>
<?php
  echo getTwitterFollowers("twitter");
?>

Make sure you cache the results, because you don't want to call this function a lot.

Upvotes: 0

wesbos
wesbos

Reputation: 26317

Just use the twitter json file like you are with the facebook API

<?php
function twitterFollowers() {
$pageID = 'wesbos';
$info = json_decode(file_get_contents('http://api.twitter.com/users/' . $pageID .'.json'));
echo $info->followers_count; 
}
?>

Beware though that you should cache the json file as twitter limits you to something like 60/hour

Upvotes: 3

David Wolever
David Wolever

Reputation: 154564

Everything you need will be over at dev.twitter.com. You'll be specifically interested in the REST API's get/users/show method.

Upvotes: 1

Related Questions