URL Shortening with Coldfusion and bit.ly

Published: {ts '2010-12-04 00:00:00'}
Author: Steven Neiland
Site Url: http://www.neiland.net/article/url-shortening-with-coldfusion-and-bit-ly/

With the advent of twitter, url shortening services have become increasingly important for publishers in order to leverage twitter as a feed mechanism for driving traffic to website blogs/articles. These services take a long url which is often longer than twitters 140 character limit and reduce it to a much shorter url.

While these services allow the publisher to manually create the short url, it makes sense to automate the process as part of publishing to a blog. In this post I will outline how to use Coldfusion to automatically interface with utilizing a url shortening service.

Step 1: Subscribe to a Shortening Service

There are many different url shortening services available and most of them are free. For this post I will be using the "bit.ly" service as it is the one I am most familiar with and the API is easy to find and read, you are however free to choose which ever one you want.

Once you are subscribed to bit.ly you need to get your API key. To do this login to bit.ly and go to the following url http://bit.ly/a/your_api_key. Note this and your username.

Step 2: Send a request to the bit.ly REST API service

This is the simplest part of the process. In order to shorten our long url we utilise bit.ly's REST API. Here is the sample code given in the docs.

http://api.bit.ly/v3/shorten?login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&longUrl=http%3A%2F%2Fbetaworks.com%2F&format=json

Breaking it down we see that we are sending 4 pieces of information to the address "http://api.bit.ly/v3/shorten".

To do this from coldfusion we simply make a call using cfhttp.

Step 3: Parse the returned xml object

When we execute this we will be returned an object with several fields. The field we are interested in is the "filecontent" field which contains the xml object which holds the data we need. So our next step is to turn the returnObject.filecontent into an xmlObject which we can parse.

Note: I specified a return format of XML. If you specify json you will need to follow different step to parse the shortened url out.

Step 4: Get the shortened url from the xml object

Now that we have an xml object we need to read our short url out of it. The xml object contains several key value pairs however ones we are interested are the "status_code" and "url" values.

The status code indicates if the request was successful or not. A value of 200 indicates success. If we get a 200 code we know we have a shortened url. Check the api documentation for the meaning of other values.

Use the following to get the data from the xmlObject.

Conclusion and Final Steps

So there you have it a simple way to automatically get a short url for use with twitter when publishing a post. From here the next step would be to take this shortened url and automatically post it to twitter.

Note: This post only covers the basics and I have not covered error handling here in any detail.