RickEpnet
asked on
Twitter API with Coldfusion
All I want to do is pull RSS feeds from many Twitter users of their tweets. I seem to now need to use the API to authenticate. I need a simple way to Authenticate then then pull down 50 or so tweets from 50 users. I just take the first tweet of each user.
I have this programed already but it did not use the API authentication. I have my API key.
I have this programed already but it did not use the API authentication. I have my API key.
I loop through this code in the past to pull down the tweets.
<cfhttp url="http://twitter.com/statuses/user_timeline/89762861.rss" method="get" />
<cfset objRSS = xmlParse(cfhttp.filecontent)>
Then a break down the XML
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
With this set of functions you will get the x last post of an specific user
<!--- ! function: getstatus(tu,tp,user/id,se ction,page ,since,lim it) --->
<!---
get status
tu = twitter username
tp = twitter password
user = screen_name to return status for
id = userid to return status for
section = status/timeline/mentions
page = page number to return
since = results since this id
limit = limit return results
--->
<cffunction name="getstatus">
<cfargument name="tu" type="string" required="yes">
<cfargument name="tp" type="string" required="yes">
<cfargument name="user" type="string" required="yes">
<cfargument name="section" type="string" required="yes">
<cfargument name="page" type="numeric" required="no">
<cfargument name="since" type="numeric" required="no" />
<cfargument name="limit" required="no" type="numeric">
<cfparam name="usertype" default="screen_name">
<cfset baseURL = "http://twitter.com/statuses/">
<cfswitch expression="#arguments.sec tion#">
<cfcase value="status">
<cfif isnumeric("arguments.user" )>
<cfset geturl = "user_timeline.json?user_i d=#argumen ts.id#">
<cfelse>
<cfset geturl = "user_timeline.json?screen _name=#arg uments.use r#">
</cfif>
</cfcase>
<cfcase value="timeline">
<cfset geturl = "friends_timeline.json?tem p=0">
</cfcase>
<cfcase value="mentions">
<cfset geturl = "mentions.json?temp=0">
</cfcase>
</cfswitch>
<cfif isdefined("arguments.limit ")>
<cfset geturl = geturl & "&count=#arguments.limit#" >
</cfif>
<cfif isdefined("arguments.page" )>
<cfset geturl = geturl & "&page=#arguments.page#">
</cfif>
<cfif isdefined("arguments.since ")>
<cfset geturl = geturl & "&since=#arguments.since#" >
</cfif>
<cfset geturl = baseurl & geturl>
<cfhttp url="#geturl#" method="get" username="#arguments.tu#" password="#arguments.tp#" result="statusmsg" />
<cfif statusmsg.statuscode eq "200 ok">
<cfset statusout = deserializejson(statusmsg. fileconten t)>
<cfelse>
<cfset statusout = arraynew(1)>
<cfset statusout[1] = "fail">
<cfset statusout[2] = statusmsg.statuscode>
</cfif>
<cfreturn statusout>
</cffunction>
<cffunction name="parseTweet" access="public" output="false" returntype="string" hint="Parse Twitter Usernames, Hashtags and URLs in a tweet">
<cfargument name="tweet" type="string" required="true" hint="The Tweet" />
<cfset var LOCAL = structNew() />
<cfset LOCAL.tweet = ARGUMENTS.tweet />
<!--- Parse the URLs as Links to the resource --->
<cfset LOCAL.tweet = REReplace(LOCAL.tweet,
'([A-Za-z]+:\/\/[A-Za-z0-9 -_]+\.[A-Z a-z0-9-_:% &\?\/. =]+)',
'<a href="\1" target="_blank" rel="nofollow">\1</a>',
'ALL')
/>
<!--- Parse the Usernames as Links to Twitter --->
<cfset LOCAL.tweet = REReplace(LOCAL.tweet,
'[@]+([A-Za-z0-9-_]+)',
'<a href="http://twitter.com/\1" title="Lookup user @\1 on Twitter" target="_blank" rel="nofollow">@\1</a>',
'ALL')
/>
<!--- Parse Hashtags as Links to Twitter�s Search --->
<cfset LOCAL.tweet = REReplace(LOCAL.tweet,
'[##]+([A-Za-z0-9-_]+)',
'<a href="http://search.twitter.com/search?q=%23\1" title="Search for ##\1 hashtag on Twitter" target="_blank" rel="nofollow">##\1</a>',
'ALL')
/>
<cfreturn LOCAL.tweet />
</cffunction>
<!---getstatus(tu,tp,user/ id,section ,page,sinc e,limit)-- ->
<strong>last 10 Tweets:</strong><br />
<br />
<cfset statusresult = getstatus([youruser],[your pass],'[th euserid]', 'status',0 ,0,'10')>
<!---<cfdump var="#statusresult#">--->
<cfloop array="#statusresult#" index="k">
<cfoutput>
<span class="Stil1"><small>#k.cr eated_at#< /small></s pan><br />
#parseTweet(k.text)#<br /><br />
</cfoutput>
</cfloop>
</cfif>
<!--- ! function: getstatus(tu,tp,user/id,se
<!---
get status
tu = twitter username
tp = twitter password
user = screen_name to return status for
id = userid to return status for
section = status/timeline/mentions
page = page number to return
since = results since this id
limit = limit return results
--->
<cffunction name="getstatus">
<cfargument name="tu" type="string" required="yes">
<cfargument name="tp" type="string" required="yes">
<cfargument name="user" type="string" required="yes">
<cfargument name="section" type="string" required="yes">
<cfargument name="page" type="numeric" required="no">
<cfargument name="since" type="numeric" required="no" />
<cfargument name="limit" required="no" type="numeric">
<cfparam name="usertype" default="screen_name">
<cfset baseURL = "http://twitter.com/statuses/">
<cfswitch expression="#arguments.sec
<cfcase value="status">
<cfif isnumeric("arguments.user"
<cfset geturl = "user_timeline.json?user_i
<cfelse>
<cfset geturl = "user_timeline.json?screen
</cfif>
</cfcase>
<cfcase value="timeline">
<cfset geturl = "friends_timeline.json?tem
</cfcase>
<cfcase value="mentions">
<cfset geturl = "mentions.json?temp=0">
</cfcase>
</cfswitch>
<cfif isdefined("arguments.limit
<cfset geturl = geturl & "&count=#arguments.limit#"
</cfif>
<cfif isdefined("arguments.page"
<cfset geturl = geturl & "&page=#arguments.page#">
</cfif>
<cfif isdefined("arguments.since
<cfset geturl = geturl & "&since=#arguments.since#"
</cfif>
<cfset geturl = baseurl & geturl>
<cfhttp url="#geturl#" method="get" username="#arguments.tu#" password="#arguments.tp#" result="statusmsg" />
<cfif statusmsg.statuscode eq "200 ok">
<cfset statusout = deserializejson(statusmsg.
<cfelse>
<cfset statusout = arraynew(1)>
<cfset statusout[1] = "fail">
<cfset statusout[2] = statusmsg.statuscode>
</cfif>
<cfreturn statusout>
</cffunction>
<cffunction name="parseTweet" access="public" output="false" returntype="string" hint="Parse Twitter Usernames, Hashtags and URLs in a tweet">
<cfargument name="tweet" type="string" required="true" hint="The Tweet" />
<cfset var LOCAL = structNew() />
<cfset LOCAL.tweet = ARGUMENTS.tweet />
<!--- Parse the URLs as Links to the resource --->
<cfset LOCAL.tweet = REReplace(LOCAL.tweet,
'([A-Za-z]+:\/\/[A-Za-z0-9
'<a href="\1" target="_blank" rel="nofollow">\1</a>',
'ALL')
/>
<!--- Parse the Usernames as Links to Twitter --->
<cfset LOCAL.tweet = REReplace(LOCAL.tweet,
'[@]+([A-Za-z0-9-_]+)',
'<a href="http://twitter.com/\1" title="Lookup user @\1 on Twitter" target="_blank" rel="nofollow">@\1</a>',
'ALL')
/>
<!--- Parse Hashtags as Links to Twitter�s Search --->
<cfset LOCAL.tweet = REReplace(LOCAL.tweet,
'[##]+([A-Za-z0-9-_]+)',
'<a href="http://search.twitter.com/search?q=%23\1" title="Search for ##\1 hashtag on Twitter" target="_blank" rel="nofollow">##\1</a>',
'ALL')
/>
<cfreturn LOCAL.tweet />
</cffunction>
<!---getstatus(tu,tp,user/
<strong>last 10 Tweets:</strong><br />
<br />
<cfset statusresult = getstatus([youruser],[your
<!---<cfdump var="#statusresult#">--->
<cfloop array="#statusresult#" index="k">
<cfoutput>
<span class="Stil1"><small>#k.cr
#parseTweet(k.text)#<br /><br />
</cfoutput>
</cfloop>
</cfif>
ASKER
Thanks everyone I will try these and let you know in a few days.
@dagaz_de - Nice. Is that your own code?
ASKER
You were correct. I took another look and it was an issue with my code and the way I was updating the database. I had put some code that would stop if a twitter account was not active but I should have just skipped it. Thank You.
You get rate limited if you make to many requests though.
ASKER
I only request 50 an hour so I am under their limit. You can see how it displays at http://www.pigkinu.com
OK great. Just FYI that page didnt load.
ASKER
Sorry to see it it is http://www.pigskinu.com/ucla.html
Works well. Nice site.
Try simulating a login with cfhttp and then use cfhttpparam to recycle the Set-Cookie collection from the responseHeader, as authentication, back into future requests.