Get Your Feet Wet with Bitbucket API
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 the Bitbucket 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;
<?php function BitbucketGet( $url = null, $pagelen = 100 ) { $user = 'USERNAME'; $pass = 'PASSWORD'; if ( $url == null ) { $url = 'https://api.bitbucket.org/2.0/repositories/'.$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); curl_close($curl); return json_decode($a); } ?>
This little snippet is a very basic example of how to use Bitbucket’s API. You can use this to get started and get much more advanced to access all of your information from Bitbucket. Head over to the documentation to see what you can access to leverage things to their full potential. Also head over to my Teamwork API blog post to see a similar approach to using their API. I utilized both APIs to make a quick and simple dashboard of our active projects.
I hope this helps you make sense of Bitbucket’s API.
Cheers!