So in the era of shortening URLs you lost the track what users are coming from where and what is the original source of traffic. Well there is nothing to worry since bit.ly keeps track of all your URL which are shortened by its API. Bit.ly not only shortens the long URL of your webpages or blogs but also keeps the information like userclicks, referral sites etc.
Bit.ly provides a nice graphical summary for all the URLs that are shortened but there are times when you need to access this data on your own. So I have created this simple PHP class which interacts with bit.ly API and helps you shorten the URLs and extract the stats information. You need to signup at bit.ly and get API key to interact with the API. Please replace the login name and API key in the following code where mentioned with the help of comments.
You can also download the PHP class from here, but before using it please rename it to .php
<?php class bitly { /* * variable declarations */ private $API_URL = 'http://api.bit.ly'; private $API_KEY = 'R_0da49e0a9118ff35f52f629d2d71bf07'; //change this to your API key, get one from http://bit.ly/account/your_api_key private $login = 'bitlyapidemo'; //change this to your login name private $action = array('shorten'=>'longUrl','stats'=>'shortUrl','expand'=>'shortUrl'); private $query_string,$current_action; private $URL = ''; private $version= '2.0.1'; var $result = ''; function __construct($display=false) { $this->return=$display; } function stats($short_url) { $this->current_action='stats'; return $this->handle_request($short_url); } function expand($short_url) { $this->current_action='expand'; return $this->handle_request($short_url); } function shorten($short_url) { $this->current_action='shorten'; return $this->handle_request($short_url); } function handle_request($url) { $URL = urldecode(trim($url)); $this->URL=$this->API_URL."/$this->current_action".$this->make_query_string(array($this->action[$this->current_action] => $url)); $results=json_decode($this->makeCurl()); return $results->results; } private function make_query_string($extra_param = array()) { $this->query_string=''; $this->URL=''; $this->query_string.='?apiKey='.$this->API_KEY; $this->query_string.='&version='.$this->version; $this->query_string.='&login='.$this->login; if(count($extra_param)>0) { foreach($extra_param as $key=>$value) $this->query_string.='&'.$key.'='.$value; } return $this->query_string; } private function makeCurl() { $curl = curl_init(); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_URL, $this->URL); $this->result = curl_exec($curl); curl_close($curl); if($this->return) echo $this->result ; else return $this->result; } } //class ends here ?> |
Here is how you can use the above class to perform various tasks.
<?php $url='http://www.digimantra.com/contests/free-domain-contest-digimantra/'; //specify your own url to be shortened $bit=new bitly(); //create object $result=$bit->shorten($url); //shortens a long URL $result=$result->$url; //stores result in the array $short_url=$result->shortUrl; //extract the short url from result array print_r($bit->expand($short_url)); //expand the shorten url print_r($bit->stats($short_url)); //get stats from bit.ly ?> |
You can customize above code to get what exactly do you need. Hope this helps.
Stay Digified !!
Sachin Khosla