News Archive
PhpRiot Newsletter
Your Email Address:

More information API: Bundles and Short URLs

Note: This article was originally published at Planet PHP on 18 June 2012.
Planet PHP

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)); $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 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 "