ColdFusion: Grab Yourself a TinyURL

I have been working on a new application the last few days and one of the requirements was to generate a short URL. There are many different URL shortening services out there today, but I decided to go with one of the original services, TinyURL. Although this service does not provide any sort of API, they do offer a quick and dirty HTTP post method of grabbing a short URL. Using CFHTTP in ColdFusion makes this easy, and only requires a single line of code:

<code lang="cfm[lines]"><cfhttp method="get" result="tinyURL" url="http://tinyurl.com/api-create.php?url=http://www.yourlongurl.com"></cfhttp>

All this does is call the TinyURL post URL and pass in the URL you would like shortened. The post simply returns the text that contains the shortened URL. Now that was to easy! It is a good idea to check the status code of the HTTP call. If a status of 200 is returned, then all is well. If not you can handle the error any way you would like.

Comments

comments powered by Disqus