Create a TinyURL with PHP

Written by David Walsh on Tuesday, December 23, 2008


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.


Follow via RSS Epic Discussion

Commenter Avatar December 23 / #
Rich says:

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!

Commenter Avatar December 23 / #
feir says:

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

Commenter Avatar December 24 / #

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.

Commenter Avatar December 25 / #
meir says:

use file_get_contents:

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

Commenter Avatar January 04 / #

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

Commenter Avatar January 13 / #
Daniel says:

Why not use this

Commenter Avatar January 13 / #
Daniel says:

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

Commenter Avatar January 27 / #
php kursu says:

thank you

Commenter Avatar March 26 / #
tx8 says:

Clear, fast and easy.Thank you :)

Commenter Avatar April 14 / #

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

Commenter Avatar May 28 / #
pampers says:

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

Commenter Avatar July 13 / #
Jerome says:

Thank you, I used it!

Commenter Avatar September 18 / #

it worked out of the box!

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

Commenter Avatar October 04 / #

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

Commenter Avatar October 15 / #
streetparade says:

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

Commenter Avatar December 04 / #

Nice job man. tks

Commenter Avatar February 02 / #
abhay says:

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

………

Be Heard!

I want to hear what you have to say! Share your comments and questions below.

Name*:
Email*:
Website:  


© David Walsh 2007-2010. Contact David Walsh. Powered by the remarkable MooTools javascript framework.