Fetching the number of followers without using any Twitter API
It is very easy to fetch the number of twitter follower of any twitter username without using any kind of Twitter API. All you need to use the DOM(Document Object Model) function available in the programming languages such as PHP, ASP.NET, etc. Here is the sample PHP functions that fetches the number of follower of the provided twitter account using DOM functions.
function string_getInsertedString($long_string,$short_string,$is_html=false){
if($short_string>=strlen($long_string))return false;
$insertion_length=strlen($long_string)-strlen($short_string);
for($i=0;$i<strlen($short_string);++$i){
if($long_string[$i]!=$short_string[$i])break;
}
$inserted_string=substr($long_string,$i,$insertion_length);
if($is_html && $inserted_string[$insertion_length-1]==’<’){
$inserted_string=’<’.substr($inserted_string,0,$insertion_length-1);
}
return $inserted_string;
}
function DOMElement_getOuterHTML($document,$element){
$html=$document->saveHTML();
$element->parentNode->removeChild($element);
$html2=$document->saveHTML();
return string_getInsertedString($html,$html2,true);
}
function getFollowers($username){
$x = file_get_contents(“http://twitter.com/”.$username);
$doc = new DomDocument;
@$doc->loadHTML($x);
$ele = $doc->getElementById(‘follower_count’);
$innerHTML=preg_replace(‘/^<[^>]*>(.*)<[^>]*>$/’,”\\1″,DOMElement_getOuterHTML($doc,$ele));
return $innerHTML;
}
Pretty simple!! Just put above lines in your functions.php and just use echo getFollowers(“twitterusername”);


Steven Willmott
Not wishing to spoil the party, but while the above might work it really isn’t a great idea to do this – there might be reasons why you wouldn’t want to use twitter’s API (e.g. rate limits) etc. – but the above can be brocken in a second if twitter decides to change it’s DOM structure. It might work for years, maybe for a month and then dead.
Twitter won’t guarantee the web page layout at all, but on the API they’ll at least give you a long heads up before they make changes that break your code.
There might be reasons to screenscrape sometimes (e.g. people who don’t have an API) but I’d try to avoid it if you can!