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开发者_JAVA百科.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.
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
Everything you need will be over at dev.twitter.com. You'll be specifically interested in the REST API's get/users/show method.
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.
精彩评论