Wrapping a call to the Twitter user lookup API with ColdFusion

This post is more than 2 years old.

I just wrote a quick UDF (available now on CFLib) that wraps the call to Twitter's user look up system. Like most Twitter APIs, this one is ridiculously simple, but I need this code for another project I'm working on (technically I need it in Flex, but the ColdFusion version helps me understand how I'll write that) so I thought I'd write it up real quick like. Here is getTwitterUser:

<cffunction name="getTwitterUser" output="false" returnType="struct"> <cfargument name="screenname" type="string" required="true"> <cfset var httpResult = "">

&lt;!--- remove the @ if they included it. ---&gt;
&lt;cfif left(arguments.screenname,1) is "@"&gt;
	&lt;cfset arguments.screenname = right(arguments.screenname, len(arguments.screenname)-1)&gt;

&lt;cfset var theUrl = "http://api.twitter.com/1/users/show.json?screen_name=#arguments.screenname#"&gt;

&lt;cfhttp url="#theUrl#" result="httpResult"&gt;
&lt;cfset var result = deserializeJSON(httpResult.filecontent)&gt;

&lt;cfreturn result&gt;	


I'd rather that be in script to be honest, but that wouldn't be difficult to rewrite. Usage then is pretty trivial. If you ask for a user that exists, you get a large structure back. If you get one that does not, you get an error key in the struct. Here's example code:

<cfset res = getTwitterUser("cfjedimaster")> <cfif structKeyExists(res, "name") and structKeyExists(res, "profile_image_url")> <cfoutput> <p> <img src="#res.profile_image_url#" align="left"> #res.name# <br clear="left"> </p> </cfoutput> </cfif>

<cfdump var="#getTwitterUser('cfjedimaster221920')#">

And the result...

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by RogerTheGeek posted on 6/7/2011 at 5:54 PM

That is a slick little function. It is added to my stash. Thanks

Comment 2 by SuperAlly posted on 6/7/2011 at 6:07 PM

Very nice Ray. I think I can make use of that too! Thanks again for all the posts!

Comment 3 by Ben Nadel posted on 6/8/2011 at 6:47 AM

It's so nice when an API is easy to use. I've used some wicked complicated ones before that seems complicated for no reason at all!

If I may make one tiny suggestion, we've been dealing with a 3rd-party API at work that is somewhat new and, at times, very confusing. One of the things that we notice in the API is that the response value is not consistent. This makes it a bit more difficult on our end (especially as those inconsistencies weren't all that well documented ahead of time).

All to say, it might be cool to give the response value some sort of standardized success flag that you set in the UDF. That way, rather than checking for an error key, the caller could, say, always check for IF (response.success) or something like that. This just adds a different kind of predictability to the response.

Comment 4 by Raymond Camden posted on 6/8/2011 at 6:55 AM

Good point. Check out the CFLib link. I added a success flag to the structure.

Comment 5 by Ben Nadel posted on 6/8/2011 at 7:01 AM

Dangy! Way to make things happen!

Comment 6 by Raymond Camden posted on 6/8/2011 at 7:02 AM

That's despite the admin of CFLib being about the worst CMS I ever built - mainly cuz I'm the only user. ;)

Comment 7 by Ben Nadel posted on 6/8/2011 at 7:04 AM

Ha ha :)