Skip to content

Instantly share code, notes, and snippets.

@roulupen
Created December 15, 2012 16:55
Show Gist options
  • Save roulupen/4297073 to your computer and use it in GitHub Desktop.
Save roulupen/4297073 to your computer and use it in GitHub Desktop.
Google API URL Shortening by ColdFusion
<cffunction name="shortenURLByGoogleAPI" access="public" returntype="Struct" hint="Takes long url as input parameter returns a struct containing both short and long url">
<cfargument name="longURL" required="true" type="string" hint="Long URL value" />
<cfset var httpReturnStruct = structNew() />
<cfset var inputParameter = {"longUrl" = arguments.longURL } />
<cfhttp url="https://www.googleapis.com/urlshortener/v1/url" method="post" result="httpReturnStruct">
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type="body" value="#serializeJSON(inputParameter)#" />
</cfhttp>
<cfreturn deserializeJSON(httpReturnStruct.Filecontent.toString()) />
</cffunction>
<cffunction name="expandURLByGoogleAPI" access="public" returntype="Struct" hint="Takes short url as input parameter returns a struct containing both short and long url">
<cfargument name="shortURL" required="true" type="string" hint="Short URL value" />
<cfset var httpReturnStruct = structNew() />
<cfhttp url="https://www.googleapis.com/urlshortener/v1/url" method="get" result="httpReturnStruct">
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type="url" name="shortUrl" value="#arguments.shortURL#" />
</cfhttp>
<cfreturn deserializeJSON(httpReturnStruct.Filecontent.toString()) />
</cffunction>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment