Create Short URLs Using U.Nu

By  on  

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!

Recent Features

  • By
    LightFace:  Facebook Lightbox for MooTools

    One of the web components I've always loved has been Facebook's modal dialog.  This "lightbox" isn't like others:  no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much."  With Facebook's dialog in mind, I've created LightFace:  a Facebook lightbox...

  • By
    Animated 3D Flipping Menu with CSS

    CSS animations aren't just for basic fades or sliding elements anymore -- CSS animations are capable of much more.  I've showed you how you can create an exploding logo (applied with JavaScript, but all animation is CSS), an animated Photo Stack, a sweet...

Incredible Demos

  • By
    Create Twitter-Style Buttons with the Dojo Toolkit

    I love that JavaScript toolkits make enhancing web pages incredibly easy. Today I'll cover an effect that I've already coded with MooTools: creating a Twitter-style animated "Sign In" button. Check out this five minute tutorial so you can take your static...

  • By
    MooTools Fun with Fx.Shake

    Adding movement to your website is a great way to attract attention to specific elements that you want users to notice. Of course you could use Flash or an animated GIF to achieve the movement effect but graphics can be difficult to maintain. Enter...

Discussion

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

  2. Now if only they had analytics tracking…

  3. Alex

    “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 :(

  4. Wow! It seems the create API adds additional whitespace / carriage return though. Might want to additionally trim() the result.
    I integrated it into PHPShortener

  5. Thank you for reporting that Guillermo! Updated.

  6. @Alex : thanks, it seems to be specific to dot nu :)

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

    function get_unu_url($url)
    {    
        $url = file_get_contents('http://u.nu/unu-api-simple?url='.urlencode($url));
        return $url;
    }
    echo get_unu_url('http://davidwalsh.name');
    

    Or was the reason because file_get_contents may not be available to everyone? I ask simply out of curiosity :)

    Thanks again!

  8. @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

  9. Thanks for the link David, that is very helpful.

  10. awesome post.

  11. You may want to note in your post that this service no longer appears to exist.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!