Bit.ly API: Bundles and Short URLs

I am a huge fan of bit.ly and use their tools for a wide variety of different things. They recently did a big relaunch with some lovely new features, which are for the most part pretty good, but which are inaccessible in places. In particular, it seems that there aren't any short URLs for the bundles - which is annoying for me as I use that feature a lot!

To get around this, I used their API to make a page which lists my bit.ly bundles, and creates shortlinks for each of them (once you've created a shortlink for a given URL once, bit.ly just re-uses the same ones the next time you ask to shorten the same URL, so this is less silly than it sounds).

In case the code is helpful, I thought I'd share.

Access to the Bit.ly API

Bit.ly has OAuth set up but I was prototyping this from the commandline so I used their basic Auth instead. This is essentially a Resource Owner Credentials flow in OAuth2 terms - supply a simple username and password to a given URL and get an access token for that user's data in response. I did this outside my application and then placed this at the top of the file to remind me what to do:

/*
 * To request access token:
 * curl -u "username:password" -X POST "https://api-ssl.bitly.com/oauth/access_token"
 */

Once you have your access token, store it in $access_token and request all your bundles by doing the following (I'd like to congratulate myself on realising that I don't want to publish my actual access token in this post!):

$bundlesRequest = new HttpRequest('https://api-ssl.bitly.com/v3/user/bundle_history');
$bundlesRequest->addQueryData(array('access_token' => $access_token));
$bundlesRequest->send();
$bundlesResult = json_decode($bundlesRequest->getResponseBody(), TRUE);

aside: All the code samples here use the pecl_http extension - if you don't have that, then check out my earlier post on different ways to POST requests from PHP, which should give you some other options.

The documentation for all the bit.ly API stuff is really very good, here's the documentation for the user/bundle_history, for example, and this told me everything I needed to know about understanding the response. In my template, I just iterate over $bundlesResult['data']['bundles']:

if(is_array($bundlesResult['data']['bundles'])) {
    echo "<ul>";
    foreach($bundlesResult['data']['bundles'] as $bundle) {
        echo "<li>" . $bundle['title'];
        if(isset($bundle['user_link'])) {
            $link = $bundle['user_link'];
        } else {
            // make the link! (Yes, in the middle of a template)
            $link = getShortLink($bundle['bundle_link']);
        }
        echo ' <a href="' . $link . '">' . $link . "</a>";
        echo "</li>\n";
    }
    echo "</ul>";
}

The docs indicate that this data can contain a user-generated link, but none of my records actually do that (I'm not sure why, anyone with any more information please leave me a comment) - but I really do need a short URL for these bundles. So, if there's no user_link, I just quickly whip one up, using the /shorten method in bit.ly's API! Here's the detail of that function (you'll need to make your $access_token available to this method for this to work):

function getShortLink($url) {
    $shortRequest = new HttpRequest('https://api-ssl.bitly.com/v3/shorten');
    $shortRequest->addQueryData(array('access_token' => $access_token,
        'longUrl' => $url));
    $shortRequest->send();
    $shortResult = json_decode($shortRequest->getResponseBody(), TRUE);
    return $shortResult['data']['url'];
}

And there I have it: one page of bundles with shortlinks, added as a new feature from my usual panicboard.

This reflects the info that I see in https://bitly.com/u/lornajane/bundles but with the shortlinks so that I can easily share them, for example on printed slides. If you're working with the bit.ly API too, I'd be interested to hear your experiences and comments - they've made some changes in line with the site rewrite but I liked what I saw.

Leave a Reply

Please use [code] and [/code] around any source code you wish to share.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>