Skip to content

Instantly share code, notes, and snippets.

@stevereich
Created September 27, 2012 05:43
Show Gist options
  • Save stevereich/3792389 to your computer and use it in GitHub Desktop.
Save stevereich/3792389 to your computer and use it in GitHub Desktop.
Coldfusion Functions for Google URL Shortener API
<cfscript>
component output="false" {
public googleURL function init(apiKey)
description="Initialize this CFC as an object"
{
variables.apiKey = arguments.apiKey;
return this;
}
public string function ShortenURL(required string url)
description="You can call this method to shorten a URL with goo.gl."
{
var httpService = new http();
httpService.setMethod("post");
httpService.setCharset("utf-8");
httpService.addParam(type="header",name="Content-Type",value="application/json");
httpService.addParam(type="body",value="{""longUrl"": ""#arguments.url#""}");
var result = httpService.send().getPrefix();
result = deserializejson(result.filecontent);
return result.id;
}
public string function ExpandURL(required string url)
description="You can call this method to expand any goo.gl short URL."
{
var httpService = new http();
httpService.setMethod("get");
httpService.setCharset("utf-8");
httpService.setUrl("https://www.googleapis.com/urlshortener/v1/url?key=" & variables.apiKey & "&shortUrl=" & arguments.url);
var result = httpService.send().getPrefix();
result = deserializejson(result.filecontent);
return result.longUrl;
}
public struct function GetAnalytics(required string url)
description="Look up a short URL's analytics,"
{
var httpService = new http();
httpService.setMethod("get");
httpService.setCharset("utf-8");
httpService.setUrl("https://www.googleapis.com/urlshortener/v1/url?key=" & variables.apiKey & "&shortUrl=" & arguments.url & "&projection=FULL");
var result = httpService.send().getPrefix();
result = deserializejson(result.filecontent);
return result;
}
}
</cfscript>
<!--- To use, create googleUrl.cfm and add this code --->
<cfscript>
// Initialize goog_url.cfc as an object
variables.googleUrlFunctions = CreateObject("component","googleURL").init(API_KEY);
// Get Shortened Url
variables.getShorty = variables.googleUrlFunctions.ShortenURL("http://www.thisisaverylongurl.com/that.has?awholelotofurlparameters=too");
// Make it Long Again
variables.sizeMatters = variables.googleUrlFunctions.ExpandURL("http://goo.gl/dF8W5");
// See if anyone cares
variables.whoCares = variables.googleUrlFunctions.GetAnalytics("http://goo.gl/dF8W5");
</cfscript>
<!--- Display Output --->
<cfoutput>
<p>Shortened: #variables.getShorty#<br>
Expanded: #variables.sizeMatters#</p>
<cfdump var="#variables.whoCares#" label="Short URL Analytics Data">
</cfoutput>
@JamoCA
Copy link

JamoCA commented Apr 7, 2015

The "ShortenURL" function is missing the setURL call and won't work without it.

        httpService.setUrl("https://www.googleapis.com/urlshortener/v1/url?key=" & variables.apiKey);

Due to an intermittent CF bug, I also recommend adding ".toString()" as ColdFusion sometimes returns content incorrectly as "binary" instead of plain text.

        result = deserializejson(result.filecontent.toString());

@JamoCA
Copy link

JamoCA commented Mar 31, 2018

FYI: According to this announcement, Google is shutting down its goo.gl URL shortening service on April 13, 2018.
https://developers.googleblog.com/2018/03/transitioning-google-url-shortener.html

This article recommends moving to bit.ly or ow.ly:
https://www.engadget.com/2018/03/30/google-shutting-down-goo-gl-url-shortening-service/

Here's a CFC for generating bit.ly shortened URLs:
https://github.com/coldfumonkeh/bit.ly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment