PHP Bit.ly API v3 Shorten and expand URLs tutorial

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

bit.ly api key

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

  1. /**
  2. * Released under GPL
  3. * This is a simple class that gives you options to use bitly's API method
  4. * /v3/expand and /v3/shorten
  5. * @access public
  6. * @author Jaspreet Chahal
  7. * @copyright Jaspreet Chahal
  8. * @version 0.1a
  9. * @link http://jaspreetchahal.org
  10. * @package bitly
  11. */
  12.  
  13. class bitly {
  14. private $_username = null;
  15. private $_apikey = null;
  16. private $_format = null;
  17. // format can be json,xml,txt
  18. // read more here http://dev.bitly.com/formats.html
  19. private $_apiurl = null;
  20. public function __construct($username,$apikey,$secure = false,$format = 'txt'){
  21. $this->_username = $username;
  22. $this->_apikey = $apikey;
  23. $this->_format = $format;
  24. if($secure) {
  25. $this->_apiurl = 'https://api-ssl.bitly.com';
  26. }
  27. else {
  28. $this->_apiurl = 'http://api.bitly.com';
  29. }
  30. }
  31.  
  32. /*
  33. Read more: http://dev.bitly.com/links.html#v3_shorten
  34. */
  35. public function shortenURL($urltoshorten) {
  36. // check here if the URL is valid or not
  37. $bitlyconnector = $this->_apiurl.'/v3/shorten?login='.$this->_username.'&apiKey='.$this->_apikey.'&uri='.urlencode($urltoshorten).'&format='.$this->_format;
  38. return $this->http($bitlyconnector);
  39. }
  40.  
  41. /*
  42. Read more: http://dev.bitly.com/links.html#v3_expand
  43. */
  44. function longURL($urltolongify) {
  45. $bitlyconnector = $this->_apiurl.'/v3/expand?login='.$this->_username.'&apiKey='.$this->_apikey.'&shortUrl='.urlencode($urltolongify).'&format='.$this->_format;
  46. return $this->http($bitlyconnector);
  47. }
  48.  
  49. /*CURL is a library you should prefer in all cases when considering
  50.   cross domain calls
  51. */
  52. function http($bitlyconnector) {
  53. $ch = curl_init();
  54. $timeout = 10;
  55. curl_setopt($ch,CURLOPT_URL,$bitlyconnector);
  56. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  57. curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
  58. $response = curl_exec($ch);
  59. curl_close($ch);
  60. return $response;
  61. }
  62. }

Usage

  1. // shorten a URL
  2. $bitly = new bitly('YOUR_BITLY_USER','YOUR_BITLY_API_KEY');
  3. // you can move USERNAME AND BITLY_API_KEY values to class properties itself
  4. // so that you don't have to mention them again and again on bitly class intantiation
  5. echo $short_url = $bitly->shortenURL('http://jaspreetchahal.org');
  6. // echoed http://bit.ly/P9udH1 for me
  7. echo '
  8. ';
  9. // expand our URL
  10. echo $bitly->longURL($short_url);
  11. // 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

bitly php demo

 

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.

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

Comments

  1. Thanks for the article, nice piece of code. Using it with a twitter script I wrote.

  2. Is bitly limiting their call? I getting error when reaching 100 link… :)

Speak Your Mind

*

CommentLuv badge