Create Short URLs Using U.Nu
I was recently referred to another URL shortening website called u.nu. As far as shortening services go, u.nu seems to have the field beat as far as the shortest base URL. If you're really intent on creating the shortest possible URLs, look no further than u.nu's API.
The PHP
function get_unu_url($url)
{
$url = 'http://u.nu/unu-api-simple?url='.urlencode($url);
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$url = curl_exec($ch);
curl_close($ch);
return trim($url);
}
$short_url = get_unu_url('https://davidwalsh.name');
//returns http://u.nu/92e
That's it. I love that there's no XML or JSON involved with their API. What's returned is the URL and nothing more!
![39 Shirts – Leaving Mozilla]()
In 2001 I had just graduated from a small town high school and headed off to a small town college. I found myself in the quaint computer lab where the substandard computers featured two browsers: Internet Explorer and Mozilla. It was this lab where I fell...
![Camera and Video Control with HTML5]()
Client-side APIs on mobile and desktop devices are quickly providing the same APIs. Of course our mobile devices got access to some of these APIs first, but those APIs are slowly making their way to the desktop. One of those APIs is the getUserMedia API...
![Create Keyboard Shortcuts with Mousetrap]()
Some of the finest parts of web apps are hidden in the little things. These "small details" can often add up to big, big gains. One of those small gains can be found in keyboard shortcuts. Awesome web apps like Gmail and GitHub use loads of...
![Resize an Image Using Canvas, Drag and Drop and the File API]()
Recently I was asked to create a user interface that allows someone to upload an image
to a server (among other things) so that it could be used in the various web sites my
company provides to its clients. Normally this would be an easy task—create a...
Nice snippet David ! Thanks.
But it’s strange, a domain name can have just one letter, “u” in this case ? I always think the minimum was two.
Now if only they had analytics tracking…
“Domain names can be as short as one character. A premium of €500/yr applies to name registrations of one character in length, €250 for domain names of two-character length, and €30 per year for domain names of any other length. In June, 2008, .NU Domain began permitting registration of all-numeric domain names.”
from http://en.wikipedia.org/wiki/.nu
Unfortunately all one character domains are already bought :(
Wow! It seems the create API adds additional whitespace / carriage return though. Might want to additionally trim() the result.
I integrated it into PHPShortener
Thank you for reporting that Guillermo! Updated.
@Alex : thanks, it seems to be specific to dot nu :)
Hey David,
Nice work here, I had never heard of that particular url service, thanks for that. I was going to ask though, couldn’t you shorten it down to something like this?
Or was the reason because
file_get_contents
may not be available to everyone? I ask simply out of curiosity :)Thanks again!
@Drew Douglass: You can definitely use file_get_contents(). cURL is simply PHP best practice. Here’s a good resource:
http://stackoverflow.com/questions/555523/filegetcontents-vs-curl-what-has-better-performance
Thanks for the link David, that is very helpful.
awesome post.
You may want to note in your post that this service no longer appears to exist.