Bitly is the most popular website to shorten links in the world. According to the statistics, Internet users add 80 million new links each day, and the number of clicks per month exceeds 8 billion. Using the service is easy and intuitive. The API is friendly to use and integration of the system on website is very simple.

Bitly

Bitly is most often used to generate a link to share on twitter or facebook.

We need to start by opening an account in bit.ly website. We can do this by using Facebook, Twitter, or traditionally via email.

The next step is to get our API key.

Click the link in the upper right corner with out login as „ahoy, bartlomiej.makowski”. Drop-down list will appear, then select „SETTINGS”.

Head towards the upper tabs and select „ADVANCED”.

The last item on the subpage is „Legacy API Key”. Click on the „Show Legacy API Key” and get the key.

Bitly

This information will be useful for us, when writing a function:

   function bitly($url){
        //login info 
        $login = 'LOGIN';   //your bit.ly login  
        $apikey = 'API-KEY'; //your bit.ly API key  
        $format = 'json';   //choose between json or xml  
        $version = '2.0.1';  
        //generating URL  
        $bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='
        .urlencode($url).'&login='.$login.'&apiKey='.$apikey.'&format='.$format;  
 
        //fetching url  
        $response = file_get_contents($bitly);  
        //for json  
        if(strtolower($format) == 'json')  
        {  
            $json = @json_decode($response,true);  
            return $json['results'][$url]['shortUrl'];  
        }  
        else //for xml
        {  
            $xml = simplexml_load_string($response);  
            return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;  
        }  
    }

Please write in the comments if you are using bitly on your own pages.

Related Posts Plugin for WordPress, Blogger...

Leave a comment

Twój adres email nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *