Create a TinyURL with PHP

Written by David Walsh on December 23, 2008 · 33 Comments

TinyURL is an awesome service. For those who don't know what TinyURL is, TinyURL allows you to take a long URL like "http://davidwalsh.name/jquery-link-nudging" and turn it into "http://tinyurl.com/67c4se". Using the PHP and TinyURL API, you can create these tiny URLs on the fly!

The PHP

//gets the data from a URL  
function get_tiny_url($url)  {  
	$ch = curl_init();  
	$timeout = 5;  
	curl_setopt($ch,CURLOPT_URL,'http://tinyurl.com/api-create.php?url='.$url);  
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);  
	curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);  
	$data = curl_exec($ch);  
	curl_close($ch);  
	return $data;  
}

//test it out!
$new_url = get_tiny_url('http://davidwalsh.name/php-imdb-information-grabber');

//returns http://tinyurl.com/65gqpp
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.

Comments

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

    amazing!

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

  3. Seems you can even use is.gd in much the same way http://is.gd/api.php?longurl=

    Link to the API info. http://is.gd/api_info.php
    I like is.gd a bit more than tinyurl because the urls are smaller.

  4. use file_get_contents:

    <?php
    function tinyurl($url) {
    return file_get_contents(‘http://tinyurl.com/api-create.php?url=’.$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…

    <?php
    function tiny_url($url){
    return file_get_contents(‘http://tinyurl.com/api-create.php?url=’ . $url);
    }
    $url = ‘http://www.url.com/’;
    echo tiny_url($url);
    ?>

  8. Clear, fast and easy.Thank you :)

  9. Hey David! I’ve just release an API for to.ly:

    PHP Code sample:

    function CompressURL($url) {

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, “http://to.ly/api.php?longurl=”.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(“http://twitter.com”); // Test

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

    thanksss

  11. Jerome July 13, 2009

    Thank you, I used it!

  12. it worked out of the box!

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

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

  14. streetparade October 15, 2009

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

  15. Nice job man. tks

  16. 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
    curl_init();

    ………

  17. Person0001 February 22, 2010

    This works great.

  18. Here is short URL which i recommend you all: http://go.af , its called go air force.

  19. bigmander April 15, 2010

    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 “http://www.xxx.com” and with this kind of one “http://www.xxx.com/xxx.php”, what do you suggest me to solve this!

    i summon all the internet gurus!!!

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

  21. 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!)

  22. myron May 12, 2011

    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…

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

  24. 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!

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

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

  26. [...] Create a TinyURL with PHPDec 23, 2008 … 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 … [...]

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

  28. Ashvin Gargav July 31, 2012

    oh devid you are my life saviour..
    Thanks.

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

    http://tinyurl.com/create.php?source=indexpage&url=http%3A%2F%2Femailhider.com%2Fremailer.php%3Fadd%3Demail%26dom%3Dmydomain%26ext%3Dcom&submit=Make+TinyURL%21&alias=

    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 May 8, 2013

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

Be Heard

Tip: Wrap your code in <pre> tags or link to a GitHub Gist!

Use Code Editor
Older
Startup Schwag: A Web 2.0 Goody Bag
Newer
MooTools Event Delegation