• Home
  • Services
  • Profile
  • Portfolio
  • Contact
  • Links
  • Blog
  • Archives
  • Sitemap
Subscribe: Posts | Comments | E-mail
  • Ajax XMLHttpRequestAsynchronous HTTP Request How To's
  • FreebiesBest Freebies Collection
  • MiscellaneousNews, Announcements & General Stuffs
  • Print & Web DesignPhotoshop, Vector, CSS, XHTML
  • ProgrammingProgramming PHP, ASP, .NET and more

Bali Web Design

Posted on September 24, 2009 - by webmaster

PHP Class Create Short URL via TinyURL, Is.gd, Hex.io, Tr.im & Bit.ly API

Programming Tutorials

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');
?>
Share this article:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • MySpace
  • Ping.fm
  • Pownce
  • Slashdot
  • StumbleUpon
  • Technorati
  • Tumblr
  • TwitThis
This entry was posted on Thursday, September 24th, 2009 at 02:39 and is filed under Programming, Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

16 Comments

We'd love to hear yours!



  1. Visit My Website

    September 26, 2009

    Permalink

    Cody said:

    Nice PHP script, very useful and I love how you incorporated tinyur.



  2. Visit My Website

    September 29, 2009

    Permalink

    Create Shorten URL on the fly with API, VBScript & PHP « Bali Web Design Studio said:

    [...] both version shortening URL via PHP and shortening URL via Classic ASP/VBScript, it is fully OOP approach and it will be easy to add [...]



  3. Visit My Website

    November 28, 2009

    Permalink

    Lea said:

    This is one thing that is being use now by most programmers to make url’s short. Good codes!



  4. Visit My Website

    December 2, 2009

    Permalink

    Web Development Company said:

    Great post! Thanks for the script, this is going to be very useful.



  5. Visit My Website

    December 4, 2009

    Permalink

    Jake Rocheleau said:

    Really nice site, great post thanks for sharing your code!



  6. Visit My Website

    January 7, 2010

    Permalink

    jooria said:

    thanks its great
    and has new idea



  7. Visit My Website

    February 24, 2010

    Permalink

    Web Development said:

    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 !



  8. Visit My Website

    April 15, 2010

    Permalink

    Moda said:

    Really nice sharing, thanks!



  9. Visit My Website

    April 25, 2010

    Permalink

    bangladesh freelance said:

    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



  10. Visit My Website

    May 2, 2010

    Permalink

    Baker said:

    wao, that was really helpful, nice article and thanks for sharing.



  11. Visit My Website

    July 14, 2010

    Permalink

    40+ Must-Bookmark PHP Classes & Libraries For Developing Faster | Site Design Tips said:

    [...] [...]



  12. Visit My Website

    July 15, 2010

    Permalink

    40+ Must-Bookmark PHP Classes & Libraries For Developing Faster « Coding Resource said:

    [...] Short URL Class [...]



  13. Visit My Website

    July 15, 2010

    Permalink

    45+ Best free PHP classes and libraries for Faster Development | Programming Scripts | Online News & Entertainment said:

    [...] Short URL Class [...]



  14. Visit My Website

    July 15, 2010

    Permalink

    45+ Best free PHP classes and libraries for Faster Development – kamran Bhutto said:

    [...] Short URL Class [...]



  15. Visit My Website

    July 16, 2010

    Permalink

    45 + Best free PHP classes and libraries for faster development | Technology Blog said:

    [...] Short URL Class [...]



  16. Visit My Website

    August 27, 2010

    Permalink

    Rob said:

    Great script. Thanks for sharing!



Leave a Comment

Here's your chance to speak.

  1. Name (required)

    Mail (required)

    Website

    Message

  • Most Popular Posts

    • { 108 } ResponsesPHP ajax login form using Jquery
    • { 79 } Responsesphp ajax tutorial create ajax based login form using jquery
    • { 71 } ResponsesASP Classic Programming Still Alive
    • { 53 } Responses63+ best practice to optimize PHP code performances
    • { 44 } ResponsesVideobox: Lightbox for videos
  • Latest Posts

    • PHP Newbie: Proper handling of Error & Exception in PHP
    • Why SEO lost importance
    • jQuery Mobile Announced : Touch-Optimized Web Framework for Smartphones & Tablets
    • Multiple Google Account Signin
    • Install, Configure & Running Memcache in Windows as Service
    • DynamicWP Image Cube Wordpress Plugin
    • New Wordpress 3.0 API menu_page_url
    • Wordpress 3.0 Custom Taxonomy
    • JQuery DataGrid Plugin Compass
    • Upgrading to upcoming CodeIgniter 2.0
  • Categories

    • advertorial
    • Ajax XMLHttpRequest
    • Featured
    • Freebies
    • Miscellaneous
    • Print & Web Design
    • Programming
    • Tutorials
    • Web 2.0
  • Featured News

    • PHP Newbie: Proper handling of Error & Exception in PHP by webmaster on August 18, 2010
    • Why SEO lost importance by webmaster on August 16, 2010
    • jQuery Mobile Announced : Touch-Optimized Web Framework for Smartphones & Tablets by webmaster on August 16, 2010
    • Install, Configure & Running Memcache in Windows as Service by webmaster on June 27, 2010
    • Website Optimization Best Practices, speed up your website load by ruby on May 21, 2009
  • Tag Cloud

    • Actionscript advertorial Ajax XMLHttpRequest API best practices CakePHP chat Classic ASP CMS CSS Development facebook firefox Flash flickr Freebies gmail HTML icons Javascript Jobs jQuery jQuery Plugins Library Lightbox memcache Mootools news Personal PHP PHP Frameworks plurk Print & Web Design prism Prototype regex regular expression Scriptaculous SEO Tutorial twitter Web 2.0 Wordpress Works YUI
  • Archives

    • August 2010
    • June 2010
    • May 2010
    • September 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • October 2007
copyleft 2007 - 2010 Bali Website Design Studio