1 Twitter Count [PHP] Thu Jul 01, 2010 12:03 am
RockerMan
Technician
Twitter Followers Widget
This is a cool little widget that you can use to count your twitter followers, just follow me and you can't go wrong
Lets get started with the PHP...
- Code:
<?php
$tw = get_option("twitterfollowerscount");
if ($tw['lastcheck'] < ( mktime() - 3600 ) )
{
$xml=file_get_contents('http://twitter.com/users/show.xml?screen_name=YOUR-FEED');
if (preg_match('/followers_count>(.*)</',$xml,$match)!=0) {
$tw['count'] = $match[1];
}
$tw['lastcheck'] = mktime();
update_option("twitterfollowerscount",$tw);
}
echo $tw['count'];
?>
OK so you looking at the code and thinking what the ****, here is a simple explanation
The first couple of lines grab the twitter count, notice the “mktime 3600″. This means the twitter count will get called every 3600 seconds or every hour instead of calling it on every page load
The next couple of lines is your twitter feed
- Code:
$xml=file_get_contents('http://twitter.com/users/show.xml?screen_name=YOUR-FEED');
Where you see YOUR-FEED replace it with your feed, here is an example:
- Code:
$xml=file_get_contents('http://twitter.com/users/show.xml?screen_name=RockerMan');
OK so now save your file as twitter.php and place it into your website directory.
Now inside your templates place this code where you would like to display the widget:
- Code:
<?php include("twitter.php"); ?>
You can easily change the way it looks by using CSS, here is an example.
HTML:
- Code:
<p><span class="twitter-followers"><?php include("twitter.php"); ?></span> Followers</p>
CSS:
- Code:
p{
color:#fff;
}
span.twitter-followers {
font-size: 24px;
vertical-align: middle;
padding-left: 5px;
color: #1e96db;
}
Hope you enjoy!
RockerMan