Create a TinyURL with PHP

By  on  

TinyURL is an awesome service. For those who don't know what TinyURL is, TinyURL allows you to take a long URL like "" and turn it into "". Using the PHP and TinyURL API, you can create these tiny URLs on the fly!


//gets the data from a URL  
function get_tiny_url($url)  {  
	$ch = curl_init();  
	$timeout = 5;  
	$data = curl_exec($ch);  
	return $data;  

//test it out!
$new_url = get_tiny_url('');

echo $new_url

Simply provide the URL and you'll received the new, tiny URL in return. If you use Twitter, you'll have noticed that Twitter automates tiny URL creation for URLs in tweets.

Track.js Error Reporting

Upcoming Events

Recent Features

  • Create a CSS Cube

    CSS cubes really showcase what CSS has become over the years, evolving from simple color and dimension directives to a language capable of creating deep, creative visuals.  Add animation and you've got something really neat.  Unfortunately each CSS cube tutorial I've read is a bit...

  • Introducing MooTools Templated

    One major problem with creating UI components with the MooTools JavaScript framework is that there isn't a great way of allowing customization of template and ease of node creation. As of today, there are two ways of creating: new Element Madness The first way to create UI-driven...

Incredible Demos

  • HTML5’s placeholder Attribute

    HTML5 has introduced many features to the browser;  some HTML-based, some in the form of JavaScript APIs, but all of them useful.  One of my favorites if the introduction of the placeholder attribute to INPUT elements.  The placeholder attribute shows text in a field until the...

  • Using Opacity to Show Focus with MooTools

    I'm a huge fan of using subtle effects like link nudging (jQuery, MooTools) to enhance the user experience and increase the perceived dynamism of my websites. Trust me -- a lot of little things are what take websites to the next level.


  1. Rich

    David, great post as always. you are starting to get me creeped out, because I was thinking about something like this yesterday. and now I dont have to reinvent the wheel.


  2. Thanks a lot for this trick. It is very usable.

  3. Seems you can even use in much the same way

    Link to the API info.
    I like a bit more than tinyurl because the urls are smaller.

  4. meir

    use file_get_contents:

    function tinyurl($url) {
       return file_get_contents(''.$url);
  5. I’m so confused
    I tested it out on my site and it works great.
    But where is it getting the $url from?

  6. Why not use this

  7. Sry…

    function tiny_url($url){
        return file_get_contents('' . $url);
    $url = '';
    echo tiny_url($url);
  8. thank you

  9. tx8

    Clear, fast and easy.Thank you :)

  10. Hey David! I’ve just release an API for

    PHP Code sample:

    function CompressURL($url) {

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, “”.urlencode($url));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_HEADER, 0);

    $html = curl_exec ($ch);
    curl_close ($ch);

    return $html;


    echo CompressURL(“”); // Test

  11. hai alll….thanks for the tricksss…. if i use daniel’s sample….my page take a bit long to load….. hmmm any other way to prevent this???


  12. Jerome

    Thank you, I used it!

  13. it worked out of the box!

    next improvement – tinyurl lets us “customize” the url with an optional alias – think we can add this somehow?

  14. @Daniel: It works fine. But when the server has diabled the file_get_contents() it will create problems. I had the same problem. But when i used cURL() it worked fine.

  15. streetparade

    Hello Thanks for the tip. Tinyurl is very usable , it doesnt need a api key, needs one so i use tinyurl ;-)

  16. Nice job man. tks

  17. abhay

    I tried using it on my local host…
    but it doesnt seem’s to be working… and yes im connected to the internet… it gives error with the


    • Anirudh

      Did you find a solution to this ? I am running into the same problem. I think we need to include the tiny url api library. I am not sure.

  18. Person0001

    This works great.

  19. Here is short URL which i recommend you all: , its called go air force.

  20. bigmander

    hi fellows
    first of all this is a great post, and i will include this to my website, but there’s a little problem this function only works with an url with the next format “” and with this kind of one “”, what do you suggest me to solve this!

    i summon all the internet gurus!!!

  21. It’s useful for me! thx~~

  22. jeff

    Nice job, Dave.
    It works as a charm.

    Did you notice TinyURL re-assigns the same short-url on duplicate URLs? (which is the behaviour I needed, indeed!)

  23. myron

    Works really nice, Thanks…

    I’m combining this with the Google QR Code API to generate qr images for a shopping cart I am developing for a school project…

  24. Liz

    Total noob question, but where does the code go exactly? Above every php file?

  25. mark

    liz –

    you can simply copy/paste this block of code inside of the

    and it should work right out of the box. just cut-paste the example URL to whatever you want.

    be sure to check out his php-IMAP interface too – its pretty awesome!

    • Liz

      Awesome! It really works…. so cool! Thanks, and I most certainly will. XD

  26. thank you so much!
    had a problem with the file_get_content…
    this works great!

  27. The $url is an argument that you define when you call the function.

  28. Ashvin Gargav

    oh devid you are my life saviour..

  29. I was testing it, and it seems that just a query string is enough to generate a tiny url.

    This is a method I am playing with to hide email addresses from spambots…

    Your script is quite cool though… as this thread shows, there are about 1000 ways to solve a problem and it’s often a matter of personal preference as to how to do it.

    I’ll post a link back when I get the PHP email hider script working… :)

  30. jim Medcraft

    Write your own shorting code with a 302 redirect not 301 like tinyurl so you get the pagerank benefits

  31. Hmm it seems like your website ate my first comment (it was extremely long)
    so I guess I’ll just sum it up what I submitted and say,
    I’m thoroughly enjoying your blog. I too am an aspiring blog writer
    but I’m still new to the whole thing. Do you have any helpful hints
    for novice blog writers? I’d certainly appreciate it.

  32. Jayden

    Hi, i got a problem on this tiny url example. At first it was generated to short url to me, but after i had tried on around hundred times in 1 day cause i’m testing on my development, it was not function after that. It return back blank data to me. So after few days, i tried again it is working fine. I’m wondering what is the problem on this? Is it have any quota on tinyurl or tinyurl website server down itself cause my problem ??

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