ColdFusion UDFs for Google URL Shortening service

This post is more than 2 years old.

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:

<cffunction name="googleURLShorten" output="false" returnType="string"> <cfargument name="url" type="string" required="true"> <cfset var httpResult = ""> <cfset var result = "">

&lt;cfset var body = {"longUrl"=arguments.url}&gt;

&lt;cfset body = serializeJson(body)&gt;

&lt;cfhttp url="https://www.googleapis.com/urlshortener/v1/url" method="post" result="httpResult"&gt;
	&lt;cfhttpparam type="header" name="Content-Type" value="application/json"&gt;
	&lt;cfhttpparam type="body" value="#body#"&gt;
&lt;/cfhttp&gt;
&lt;cfset result = httpResult.filecontent.toString()&gt;
&lt;cfreturn deserializeJSON(result).id&gt;

</cffunction>

And then the reverse:

<cffunction name="googleURLExpand" output="false" returnType="string"> <cfargument name="url" type="string" required="true"> <cfset var httpResult = ""> <cfset var result = "">

&lt;cfhttp url="https://www.googleapis.com/urlshortener/v1/url?shortUrl=#urlEncodedFormat(arguments.url)#" method="get" result="httpResult"&gt;&lt;/cfhttp&gt;

&lt;cfset result = httpResult.filecontent.toString()&gt;
&lt;cfreturn deserializeJSON(result).longUrl&gt;

</cffunction>

And a quick test script:

<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>

<cfset reversed = googleURLExpand(test)> <cfoutput> I expanded it to #reversed#. </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. ;)

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 andy matthews posted on 1/11/2011 at 3:48 AM

Sweet! I've been waiting for this to come out so that I could add it to Shrinkadoo!

http://www.andymatthews.net...

Comment 2 by SuperAlly posted on 1/11/2011 at 3:50 AM

That's very cool Ray, thanks again. I was just asked about this the other day.

Comment 3 by Wookie posted on 1/13/2011 at 10:25 PM

Brilliant! Love your "5 minutes" of work.

Comment 4 by Raymond Camden posted on 1/13/2011 at 10:52 PM

I've posted both to CFLib.org.

Comment 5 by Phillip Senn posted on 1/15/2011 at 1:02 AM

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