Create a TinyURL with PHP
TinyURL is an awesome service. For those who don't know what TinyURL is, TinyURL allows you to take a long URL like "https://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('https://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.
![CSS Animations Between Media Queries]()
CSS animations are right up there with sliced bread. CSS animations are efficient because they can be hardware accelerated, they require no JavaScript overhead, and they are composed of very little CSS code. Quite often we add CSS transforms to elements via CSS during...
![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...
![Implement jQuery’s hover() Method in MooTools]()
jQuery offers a quick event shortcut method called hover that accepts two functions that represent mouseover and mouseout actions. Here's how to implement that for MooTools Elements.
The MooTools JavaScript
We implement hover() which accepts to functions; one will be called on mouseenter and the other...
![MooTools Kwicks Plugin]()
I wrote a post titled Get Slick with MooTools Kwicks ages ago. The post was quite popular and the effect has been used often. Looking back now, the original code doesn't look as clean as it could. I've revised the original...
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
: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…
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();
………