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 " ); } return($result); } //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"; }
Great post! Thanks for the code. I’m working on a URL shortener myself and one of the todos is an API.
Justins last blog post..Top Entrecard Droppers
Thanks,
This works great….I was eagerly searching for a code that satisfy my needs..Thank you….
It seems you use url shortinizing services.
I use them quite often and decided to stay with http://fff.to after evaluating
a number of them. It is quite fast and provides full statistics.
And there is a traffic distribtuion system, very webmaster-friendly feature.
It helps to detect better conversions for my traffic
[…] oldest and probably most well-known URL shortener on the ‘nets, for years. I even did a writeup on TinyURL’s API, because for some odd reason they make it hard to […]
Now that is an exceptionally useful snippet. Very useful for integrating twitter functionality into your site. Thanks for that!
wow, thanks for the tutorial info TinyURL API. help me to find solution fast
Thanks for your code, it really works great. I know only a little php, so I couldn’t to do this myself.
I have looked for some information of this topic for the last several month. Your blog is greatly treasured.