Hello,
I thought I should do a write up on this topic not because I like using bit.ly for various reasons but because bit.ly is a platform that you will end up using one day hopefully. That day for you is today by the way that is one reason you are reading this article.
Ok so lets not waste time to make this article lengthy and I don’t want you to loose interest half way through.
Lets do it step by step.
I assume that you’ve already created your bit.ly account and are ready to roll. Now this post assume that you are going to use bit.ly for your personal website and are not shortening URLs on behalf of your clients, that is totally different topic and I will write about it later.
Ok first up goto http://dev.bitly.com and click on Get started button.
Wow! that step was easy. Don’t stress the next steps are even easier. The dev home page talk about few things and we are just going to concentrate on shortening URLs for our site
Grab your bit.ly API key from here
http://bitly.com/a/your_api_key
Your bit.ly username and API key are shown on this page as shown below

Now that you got hold of your username and API key lets jump straight into the PHP part and see how we can shorten the URLs easily.
The simple bitly PHP class
/** * Released under GPL * This is a simple class that gives you options to use bitly's API method * /v3/expand and /v3/shorten * @access public * @author Jaspreet Chahal * @copyright Jaspreet Chahal * @version 0.1a * @link http://jaspreetchahal.org * @package bitly */ class bitly { private $_username = null; private $_apikey = null; private $_format = null; // format can be json,xml,txt // read more here http://dev.bitly.com/formats.html private $_apiurl = null; public function __construct($username,$apikey,$secure = false,$format = 'txt'){ $this->_username = $username; $this->_apikey = $apikey; $this->_format = $format; if($secure) { $this->_apiurl = 'https://api-ssl.bitly.com'; } else { $this->_apiurl = 'http://api.bitly.com'; } } /* Read more: http://dev.bitly.com/links.html#v3_shorten */ public function shortenURL($urltoshorten) { // check here if the URL is valid or not $bitlyconnector = $this->_apiurl.'/v3/shorten?login='.$this->_username.'&apiKey='.$this->_apikey.'&uri='.urlencode($urltoshorten).'&format='.$this->_format; return $this->http($bitlyconnector); } /* Read more: http://dev.bitly.com/links.html#v3_expand */ function longURL($urltolongify) { $bitlyconnector = $this->_apiurl.'/v3/expand?login='.$this->_username.'&apiKey='.$this->_apikey.'&shortUrl='.urlencode($urltolongify).'&format='.$this->_format; return $this->http($bitlyconnector); } /*CURL is a library you should prefer in all cases when considering cross domain calls */ function http($bitlyconnector) { $timeout = 10; return $response; } }
Usage
// shorten a URL $bitly = new bitly('YOUR_BITLY_USER','YOUR_BITLY_API_KEY'); // you can move USERNAME AND BITLY_API_KEY values to class properties itself // so that you don't have to mention them again and again on bitly class intantiation echo $short_url = $bitly->shortenURL('http://jaspreetchahal.org'); // echoed http://bit.ly/P9udH1 for me echo ' '; // expand our URL echo $bitly->longURL($short_url); // echoed http://jaspreetchahal.org
ALright so that’s it.
Now one more thing. Once you create a short URL you can always see it from your account and get important stats such as number of times its clicked etc as shown below

I hope that you enjoyed this post, just in case you would like to add more to the above mentioned stuff go ahead and post your comments
Cheers.





JC WordPress Coupon Revealer Plugin Pro License
Australian Street Names with City, State and Display Names only, Single Server License
Thanks for the article, nice piece of code. Using it with a twitter script I wrote.
Is bitly limiting their call? I getting error when reaching 100 link…
I am not quite sure about the number 100, but they do impose rate limits per API call per hour. They addressed this question here http://support.bitly.com/knowledgebase/articles/76784-what-are-the-api-rate-limits-how-do-i-request-add, If you have another source to share do let other readers know.
Cheers.