API: Bundles and Short URLs

I am a huge fan of 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 bundles, and creates shortlinks for each of them (once you’ve created a shortlink for a given URL once, 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 API 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 ""

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('');
$bundlesRequest->addQueryData(array('access_token' => $access_token));
$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 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 "
    "; foreach($bundlesResult['data']['bundles'] as $bundle) { echo "
  • " . $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 ' ' . $link . ""; echo "
  • \n"; } echo "
"; }

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’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('');
    $shortRequest->addQueryData(array('access_token' => $access_token,
        'longUrl' => $url));
    $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 but with the shortlinks so that I can easily share them, for example on printed slides. If you’re working with the 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.

This site uses Akismet to reduce spam. Learn how your comment data is processed.