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
Be Heard!
I want to hear what you have to say! Share your comments and questions below.











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!
Thanks a lot for this trick. It is very usable.
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.
use file_get_contents:
<?php
function tinyurl($url) {
return file_get_contents(‘http://tinyurl.com/api-create.php?url=’.$url);
}
?>
I’m so confused
I tested it out on my site and it works great.
But where is it getting the $url from?
Why not use this
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);
?>
thank you
Clear, fast and easy.Thank you :)
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
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
Thank you, I used it!
it worked out of the box!
next improvement – tinyurl lets us “customize” the url with an optional alias – think we can add this somehow?
@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.
Hello Thanks for the tip. Tinyurl is very usable , it doesnt need a api key, bit.ly needs one so i use tinyurl ;-)
Nice job man. tks
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();
………