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:
<cfset var body = {"longUrl"=arguments.url}> <cfset body = serializeJson(body)> <cfhttp url="https://www.googleapis.com/urlshortener/v1/url" method="post" result="httpResult">
<cfhttpparam type="header" name="Content-Type" value="application/json">
<cfhttpparam type="body" value="#body#">
</cfhttp>
<cfset result = httpResult.filecontent.toString()>
<cfreturn deserializeJSON(result).id>
</cffunction>
<cffunction name="googleURLShorten" output="false" returnType="string">
<cfargument name="url" type="string" required="true">
<cfset var httpResult = "">
<cfset var result = "">
And then the reverse:
<cfhttp url="https://www.googleapis.com/urlshortener/v1/url?shortUrl=#urlEncodedFormat(arguments.url)#" method="get" result="httpResult"></cfhttp> <cfset result = httpResult.filecontent.toString()>
<cfreturn deserializeJSON(result).longUrl>
</cffunction>
<cffunction name="googleURLExpand" output="false" returnType="string">
<cfargument name="url" type="string" required="true">
<cfset var httpResult = "">
<cfset var result = "">
And a quick test script:
<cfset reversed = googleURLExpand(test)>
<cfoutput>
I expanded it to #reversed#.
</cfoutput>
<cfset sampleURL = "http://www.raymondcamden.com/index.cfm/2011/1/10/jQuery-based-example-of-simple-shopping-cart-UI">
<cfset test = googleURLShorten(sampleURL)>
<cfoutput>
I shorteneded #sampleURL# to #test#.<br/>
</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. ;)
Archived Comments
Sweet! I've been waiting for this to come out so that I could add it to Shrinkadoo!
http://www.andymatthews.net...
That's very cool Ray, thanks again. I was just asked about this the other day.
Brilliant! Love your "5 minutes" of work.
I've posted both to CFLib.org.
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