Twitter: raymondcamden


Address: Lafayette, LA, USA

ColdFusion UDFs for Google URL Shortening service

01-10-2011 4,927 views ColdFusion 5 Comments

I wrote this in about five minutes and it doesn't support auth tokens yet, but here are two UDFs that make use of Google's URL Shortening API. First, the shorten call:

view plain print about
1<cffunction name="googleURLShorten" output="false" returnType="string">
2    <cfargument name="url" type="string" required="true">
3    <cfset var httpResult = "">
4    <cfset var result = "">
5
6    <cfset var body = {"longUrl"=arguments.url}>
7
8    <cfset body = serializeJson(body)>
9    
10    <cfhttp url="https://www.googleapis.com/urlshortener/v1/url" method="post" result="httpResult">
11        <cfhttpparam type="header" name="Content-Type" value="application/json">
12        <cfhttpparam type="body" value="#body#">
13    </cfhttp>
14    <cfset result = httpResult.filecontent.toString()>
15    <cfreturn deserializeJSON(result).id>
16</cffunction>

And then the reverse:

view plain print about
1<cffunction name="googleURLExpand" output="false" returnType="string">
2    <cfargument name="url" type="string" required="true">
3    <cfset var httpResult = "">
4    <cfset var result = "">
5
6    <cfhttp url="https://www.googleapis.com/urlshortener/v1/url?shortUrl=#urlEncodedFormat(arguments.url)#" method="get" result="httpResult"></cfhttp>
7
8    <cfset result = httpResult.filecontent.toString()>
9    <cfreturn deserializeJSON(result).longUrl>
10</cffunction>

And a quick test script:

view plain print about
1<cfset sampleURL = "http://www.coldfusionjedi.com/index.cfm/2011/1/10/jQuery-based-example-of-simple-shopping-cart-UI">
2<cfset test = googleURLShorten(sampleURL)>
3<cfoutput>
4I shorteneded #sampleURL# to #test#.<br/>
5</cfoutput>
6
7<cfset reversed = googleURLExpand(test)>
8<cfoutput>
9I expanded it to #reversed#.
10</cfoutput>

When run, I get:

I shorteneded http://www.coldfusionjedi.com/index.cfm/2011/1/10/jQuery-based-example-of-simple-shopping-cart-UI to http://goo.gl/qeDBv. I expanded it to http://www.coldfusionjedi.com/index.cfm/2011/1/10/jQuery-based-example-of-simple-shopping-cart-UI.

Love that typo on the first line. Anyway, the UDFs could be improved with simple error checking and optional token support. But this is the best you get in five minutes. ;)

5 Comments

  • Commented on 01-10-2011 at 4:48 PM
    Sweet! I've been waiting for this to come out so that I could add it to Shrinkadoo!

    http://www.andymatthews.net/code/Shrinkadoo/
  • SuperAlly #
    Commented on 01-10-2011 at 4:50 PM
    That's very cool Ray, thanks again. I was just asked about this the other day.
  • Wookie #
    Commented on 01-13-2011 at 11:25 AM
    Brilliant! Love your "5 minutes" of work.
  • Commented on 01-13-2011 at 11:52 AM
    I've posted both to CFLib.org.
  • Commented on 01-14-2011 at 2:02 PM
    Google says "All goo.gl URLs and click analytics are public and can be shared by anyone".
    If you go here, you (anyone) can see usage statistics:
    http://goo.gl/info/qeDBv#week

Post Reply

Please refrain from posting large blocks of code as a comment. Use Pastebin or Gists instead. Text wrapped in asterisks (*) will be bold and text wrapped in underscores (_) will be italicized.

Leave this field empty