Recently I’ve embarked on making an advanced dashboard for all of my company’s development websites. One feature that I needed to tap into was our Bitbucket repositories.

So I started diving into Bitbucket’s API, not being a master at cURL it took some trial and error but eventually I got it working as needed (I really just wanted to show the latest commits for each site).

In case you are having trouble figuring things out, I wrote a quick (easily modifiable) php function that might help you get on the right track;

function BitbucketGet( $url = null, $pagelen = 100 ) {
        $user = 'USERNAME';
        $pass = 'PASSWORD';

        if ( $url == null ) {
                $url = ''.$user.'/';
        $url = $url . '?pagelen=' . $pagelen;

        $curl = curl_init();

        // Set curl options
        curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ); 
        curl_setopt($curl, CURLOPT_USERPWD, "$user:$pass");
        curl_setopt($curl, CURLOPT_HEADER, false); 
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_URL, $url);

        $a = curl_exec($curl);

        return json_decode($a);

I hope this helps you make sense of Bitbucket’s API.