Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

Create a TinyURL with PHP

26 Responses »

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.

Discussion

  1. rich
    December 23, 2008 @ 10:29 am

    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. December 23, 2008 @ 7:44 pm

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

  3. December 24, 2008 @ 3:19 pm

    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. meir
    December 25, 2008 @ 2:32 am

    use file_get_contents:

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

  5. January 4, 2009 @ 5:45 pm

    I’m so confused
    I tested it out on my site and it works great.
    But where is it getting the $url from?

  6. January 13, 2009 @ 9:52 am

    Why not use this

  7. January 13, 2009 @ 9:53 am

    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. January 27, 2009 @ 8:56 am

    thank you

  9. tx8
    March 26, 2009 @ 11:05 am

    Clear, fast and easy.Thank you :)

  10. April 14, 2009 @ 9:11 am

    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

  11. May 28, 2009 @ 5:47 am

    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

  12. jerome
    July 13, 2009 @ 1:45 pm

    Thank you, I used it!

  13. September 18, 2009 @ 7:54 pm

    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. October 4, 2009 @ 6:41 am

    @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
    October 15, 2009 @ 2:40 pm

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

  16. December 4, 2009 @ 8:16 am

    Nice job man. tks

  17. abhay
    February 2, 2010 @ 6:10 am

    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();

    ………

  18. person0001
    February 22, 2010 @ 2:43 pm

    This works great.

  19. March 6, 2010 @ 4:33 pm

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

  20. bigmander
    April 15, 2010 @ 1:08 pm

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

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!