Author: Steven Neiland
Published:

Warning: This blog entry was written two or more years ago. Therefore, it may contain broken links, out-dated or misleading content, or information that is just plain wrong. Please read on with caution.

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

  • login: our bit.ly username
  • apiKey: our bit.ly api key
  • longUrl: The url we want to shorten
  • format: The format we want to information returned in. We can specify json or xml

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

<!---Build request string--->
<cfset bit_ly_useranme = [you username here]>
<cfset bit_ly_apikey = [you api key here]>
<cfset longUrl = [url to shorten]>
<cfset return_format = [json or xml]><!---I specified xml format here for return--->

<cfset requestUrl = "http://api.bit.ly/v3/shorten?login=#bit_ly_username#
&apiKey=#bit_ly_apikey#&longUrl=#longUrl#&format=#return_format#"
>


<cfhttp method="get" url="#requestUrl#" result="returnObject">
1 2

What Do You Think?

Reader Comments

Post a Comment

Comment Etiquette:

  • Please keep comments on-topic.
  • Please do not post unrelated questions or large chunks of code.
  • Please do not engage in flaming/abusive behaviour.
  • Comments that contain or appear to be advertisments, will not be published.
  • Comments that appear to be created for the purpose of linkbuilding to commercial sites will be removed.

We are all adults here so play nice.

*
*



Archives Blog Listing

Tag Listing

Learn CF In A Week

Treehouse

 
Fork me on GitHub