Tuesday, June 2, 2009

Getting Twitter Followers using Coldfusion

Question :
How to get the number of followers on Twitter using Coldfusion ?


Well, just use cfhttp and some string functions to get the result, I have created a function for the same which can be used, the function accepts the twitter url.

<cffunction name="twitter" access="public" returntype="any" output="no">
<cfargument name="url" required="yes" type="string">
<cfhttp
url="#url#"
method="get"
resolveurl="yes"
useragent="Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)">
</cfhttp>
<cfset stringTOCheck = '<span id="follower_count" class="stats_count numeric">'>
<cfset lenghtOfCheck = len(stringTOCheck)>
<cfset firstOccurance = Find('#stringTOCheck#','#cfhttp.FileContent#')>
<cfset secondOccurance = Find('</span>','#cfhttp.FileContent#','#firstOccurance#')>
<cfset startFrom = #firstOccurance#+#lenghtOfCheck#>
<cfset endFrom = #secondOccurance#-(#firstOccurance#+#lenghtOfCheck#)>
<cfset followers = MID('#cfhttp.FileContent#',#startFrom#,#endFrom#)>
<cfreturn followers>
</cffunction>

Enjoy !

No comments: