Using Bit.ly PHP API

Written by on August 15, 2010 in PHP - 6 Comments

URL shortening services became really popular after Twitter went mainstream. The leader in URL shortening services is definitely Bit.ly. In this tutorial i will briefly describe how to use it’s API to autmoatically sorten an URL address.

Signup for Bit.ly API key

Before we can do anything we need to signup for bit.ly api key, you can do it here http://bit.ly/a/sign_up, once you’re done you can find your key at http://bit.ly/a/your_api_key

Hint if you don’t feel like signing up you can use Bit.ly demo api key and password, but keep in mind that it shouldn’t be used in production. Login:bitlyapidemo, ApiKey=R_0da49e0a9118ff35f52f629d2d71bf07

Shortening URL with PHP and Bit.ly

Bit.ly has simple RESTful API so it’s easy to use it, in fact the whole script can be written in just one function.

function bitly_shorten($url)
{
    $query = array(
        "version" => "2.0.1",
        "longUrl" => $url,
        "login" => API_LOGIN, // replace with your login
        "apiKey" => API_KEY // replace with your api key
    );
 
    $query = http_build_query($query);
 
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://api.bit.ly/shorten?".$query);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
    $response = curl_exec($ch);
    curl_close($ch);
 
    $response = json_decode($response);
 
    if($response->errorCode == 0 && $response->statusCode == "OK") {
        return $response->results->{$url}->shortUrl;
    } else {
        return null;
    }
}

That’s it just copy and paste this code into your project.

BTW. I noticed that it’s possible to use custom domain names, instead of bit.ly or j.mp, however i did not find anything about it in Bit.ly v3 API, anyone can put some light on this? If yes, please post in the comments.

About the Author

Greg Winiarski is a freelance PHP and JavaScript programmer. He specializes in web applications and WordPress development.

6 Comments on "Using Bit.ly PHP API"

  1. Matt @ Bit.ly August 16, 2010 at 9:10 pm ·

    This is awesome! One thing: you can use our preferred V3 API endpoints by tweaking this ever-so-slightly:

    Just omit the “version” parameter and curl http://api.bit.ly/v3/shorten

    Feel free to drop me a line if you have any questions!

  2. Greg Winiarski August 17, 2010 at 9:55 pm ·

    Thanks @Matt for your input, i will check it out and update the script.

  3. Praateek October 7, 2010 at 8:41 am ·

    Well here is the script for v3:

    status_code == 200 && $response->status_txt == “OK”)
    {
    return $response->data->url;
    } else
    {
    return null;
    }

    }
    $link = urldecode(“http://praateek.com”);
    echo bitly_shorten($link);

    ?>

  4. Praateek October 7, 2010 at 8:47 am ·

    Well here is the v3 script:

    <?php

    /**
    * @author Praateek
    * @copyright 2010
    */

    function bitly_shorten($url)
    {
    $username = "praateekcom";
    $apikey = "R_0ad16fa341316d31e190219b90d28525";
    $response = file_get_contents("http://api.bit.ly/v3/shorten?login=$username&apiKey=$apikey&longUrl=$url&format=json&quot ;) ;
    $response = json_decode($response);
    if ($response->status_code == 200 && $response->status_txt == "OK")
    {
    return $response->data->url;
    } else
    {
    return null;
    }

    }
    $link = urldecode("http://praateek.com&quot ;) ;
    echo bitly_shorten($link);

    ?>

  5. Praateek October 7, 2010 at 8:48 am ·

    Well here is the v3 script
    <?php

    /**
    * @author Praateek
    * @copyright 2010
    */

    function bitly_shorten($url)
    {
    $username = "xyzxyz";
    $apikey = "R_123456";
    $response = file_get_contents("http://api.bit.ly/v3/shorten?login=$username&apiKey=$apikey&longUrl=$url&format=json&quot ;) ;
    $response = json_decode($response);
    if ($response->status_code == 200 && $response->status_txt == "OK")
    {
    return $response->data->url;
    } else
    {
    return null;
    }

    }
    $link = urldecode("http://praateek.com&quot ;) ;
    echo bitly_shorten($link);

    ?>

  6. Felix December 30, 2010 at 1:35 pm ·

    Is it possible to get the short url from bit.ly via https instead of the non-encrypted http://api.bit.ly/... ?

Leave a Comment