Anyone who tweets or sends URL’s in emails likes short URL’s for different reasons. Twitter users like ’em because of the 140 char limit for tweets.  You don’t want to use all 140 chars allocation on some hairy ugly URL–you also need a few to hint what it IS! Programmatic  email senders like ’em because they fit on one line.  Is there an API to do this under program control?

When the Tix•R•Us business was going we had a program that would send a customer an encoded and necessarily long custom link so that they could access the file containing their printable ticket.   I can’t tell you the number of support tickets we had  when some lame email client broke the long  URL into two lines and then clicking just the first line didn’t work and the customer couldn’t figure it out.

TinyURL and other crunchers of that ilk exist exactly for this purpose; in fact many third party twitter tools automatically crunch the URL’s when you use them to tweet.   You can even get a toolbar button to easily tinify any URL you might happen to be on.   I thought surely there must be an API for this, but you don’t see mention of one on TinyURL’s web at all.  I tried using curl to post to the receiver that TinyURL’s form goes to (it is called create.php) but that did not work. Then I found out they have a trivial API–they just don’t tell us about it! You have to append a  simple query string to api-create.php as shown below,  rather than POST to create.php, (i.e. you use the GET method) –even easier!

Here is a trivial PHP program to do it.   Hint: if you want to use the code, copy/paste it into your own app and the cut-off part will show.  If you don’t you probably don’t care what it is anyway.  🙂

function getTinyUrl($url) {
   if (($result = file_get_contents('http://tinyurl.com/api-create.php?url='.$url))  === false) {
        throw new Exception( "Can't tinify $url " );
//tiny calling shell, obviously you'd want to get the URL from a form in the real world
try {
    echo getTinyUrl("http://food.dorkage.net");
} catch (Exception $e) {
    echo "caught exception " ,  $e->getMessage() , "\n";

8 comments to TinyURL API

A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna, tincidunt vitae molestie nec, molestie at mi. Nulla nulla lorem, suscipit in posuere in, interdum non magna.