7,862 views

Twitter is one of the biggest social media website in the Internet today with more than 200 million users and around 400 thousand new accounts created every day. Link - How Many Users Does Twitter Have TODAY. With this massive amount of people anyone could think on how to take this as a great opportunity to increase their social network connections and somehow use it for any sort of promotional strategies for a specific brand to market a product in the internet. With that in mind, it is important to have an idea on how big is your target market on twitter even without visiting the website. Some people post their total twitter followers on their website while others use it for reference on their marketing reports. There are a lot of free twitter follower counter services in the internet and I will show you how to do it on your own. You will have full control on how you want to display it on your website or you can start your own free twitter follower counter service. I will still be using tmhOAuth PHP library just like on the other Twitter Application How to tweet from your website that were created few months ago. All related files are available at the end of this twitter tutorial.

By Rnel, on April 15, 2011, under Twitter Tutorials
 

Twitter is one of the biggest social media website in the Internet today with more than 200 million users and around 400 thousand new accounts created every day. Link - How Many Users Does Twitter Have TODAY. With this massive amount of people anyone could think on how to take this as a great opportunity to increase their social network connections and somehow use it for any sort of promotional strategies for a specific brand to market a product in the internet. With that in mind, it is important to have an idea on how big is your target market on twitter even without visiting the website. Some people post their total twitter followers on their website while others use it for reference on their marketing reports.

There are a lot of free twitter follower counter services in the internet and I will show you how to do it on your own. You will have full control on how you want to display it on your website or you can start your own free twitter follower counter service.

I will still be using tmhOAuth PHP library just like on the other Twitter Application How to tweet from your website that was created few months ago.

All related files are available at the end of this twitter tutorial.

 

Class:

 

This is a simple class that uses the 'users resources' - 'users/show' call on twitter API. It requires a twitter screen name or a twitter id and returns extended information of that specific user including the total number of followers that we need. It first determine if the given parameter is a user id or a screen name before performing the appropriate request. It will show the total number of twitter followers if the request is successful otherwise an error message will be presented.

 

	    	<php
			class RNELdotNET_getTotalTwitterFollowers{
				public function show($q){
				global $tmhOAuth;
					if($q){
						$tmhOAuth->request('GET', $tmhOAuth->url('users/show','json'), array(
							(ctype_digit($q) ? 'user_id' : 'screen_name' ) => $q,
						));
				
						if($tmhOAuth->response['code'] == 200){
						  $result = $tmhOAuth->pr(json_decode($tmhOAuth->response['response']));
						  $return = $result->followers_count;
						}
						else{
						  $result = $tmhOAuth->pr(json_decode($tmhOAuth->response['response']));
						  $return = 'Error: User ' . (ctype_digit($q) ? 'id' : 'name' ) . ' ' . $q . ' was ' . $result->error;
						}
					}
					else{
						$return = 'Error: A twitter screen name or user id is required.';
					}
					
					return $return;
				}
			}
			?>
		

 
 

Usage:

 

Include necessary files and call show() function from the class with the screen name or user id as the given parameter.

 

	    	<php
			//include tmhOAuth PHP Library
			include_once('tmhOAuth/tmhOAuth.php');
			$tmhOAuth = new tmhOAuth(array());
			
			//include RNELdotNET_getTotalTwitterFollowers PHP Class
			include_once('RNELdotNET_getTotalTwitterFollowers.php');
			$RNELdotNET_getTotalTwitterFollowers = new RNELdotNET_getTotalTwitterFollowers();
			
			echo '@RNELdotNET ' . number_format($RNELdotNET_getTotalTwitterFollowers->show('RNELdotNET')) . ' followers';
			echo '
'; echo '@Google ' . number_format($RNELdotNET_getTotalTwitterFollowers->show('google')) . ' followers'; echo '
'; echo '178785731 ' . number_format($RNELdotNET_getTotalTwitterFollowers->show('178785731')) . ' followers'; ?>

 
 

Result:

 

@RNELdotNET 1,036 followers
@Google 2,978,767 followers
178785731 1,036 followers