PHP Class Create Short URL via TinyURL, Is.gd, Hex.io, Tr.im & Bit.ly API
Short URL is commonly used today for several reasons: avoid url garbling, take smallest space especially for long url which need to be posted at limited space format eg: twitter, also often used to manipulate user for specific purposes eg: hiding orginal url for phising and ads/affiliation links.
There are many websites that provide shortening url services, tinyurl, bit.ly, hex.io are some of them, below is simple PHP script as an implementation to use their API, create short url on the fly, currently support: tinyURL, bit.ly, is.gd, tr.im and hex.io, but you can easily add other providers.
PHP Class
<?php
/*
** Title: Shortening URL Class
** Author: chazzuka <ariel@chazzuka.com>
** Author URL: http://www.chazzuka.com
** Latest Updated: September 24 2009
*/
class ShortUrl {
public static function create($url,$provider='tinyurl',$user='',$key='') {
$api_url = sprintf(self::api($provider),urlencode($url),$user,$key);
return self::inspect($provider,self::execute($api_url));
}
private static function execute($url) {
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
$text = curl_exec($ch);
curl_close($ch);
return $text;
}
private static function inspect($provider,$xml) {
if(!empty($xml)) {
switch(strtolower(trim($provider))){
case "bitly":
$o = new SimpleXMLElement($xml);
return (string)$o->results->nodeKeyVal->shortUrl;
break;
case "trim":
$o = new SimpleXMLElement($xml);
return (string)$o->url;
break;
case "isgd":
case "hexio":
default:
return $xml;
}
}
return false;
}
private static function api($provider) {
switch(strtolower(trim($provider))){
case "bitly":
$return = "http://api.bit.ly/shorten?version=2.0.1&format=xml&longUrl=%s&login=%s&apiKey=%s";
break;
case "isgd":
$return = "http://is.gd/api.php?longurl=%s";
break;
case "hexio":
$return = "http://hex.io/api-create.php?url=%s";
break;
case "digg":
$return = "http://services.digg.com/url/short/create?url=%s&appkey=%s&type=xml";
break;
case "trim":
$return = "http://api.tr.im/v1/trim_url.xml?url=%s";
break;
default:
$return = "http://tinyurl.com/api-create.php?url=%s";
}
return $return;
}
}
?>
Sample of usage:
<?php $url = 'http://www.chazzuka.com/blog/?p=192'; echo ShortUrl::create($url,'trim'); echo '<hr />'; echo ShortUrl::create($url,'tinyurl'); echo '<hr />'; echo ShortUrl::create($url,'isgd'); echo '<hr />'; echo ShortUrl::create($url,'hexio'); echo '<hr />'; echo ShortUrl::create($url,'bitly','your_user_name','your_api_key'); ?>





Hi, I am new.This is my frist post …lol.
say hello to all.
Great script. Thanks for sharing!
wao, that was really helpful, nice article and thanks for sharing.
Nice article and tutorial!
Want to head up as a freelance web programmer and designer!
Really don’t know it would be possible or not for me!
Well done!
bangladesh freelance
Really nice sharing, thanks!
Will definitely be going to try your script and will also try with other providers which you have not mentioned. Let’s see. Anyways thanks !
thanks its great
and has new idea
Really nice site, great post thanks for sharing your code!
Great post! Thanks for the script, this is going to be very useful.
This is one thing that is being use now by most programmers to make url’s short. Good codes!
Nice PHP script, very useful and I love how you incorporated tinyur.