Well hello there, dear reader, this the first article from a series of tutorial articles that will be featured around here. Let's get started then.

If  you read the title but still haven't figured out what the hell I'm talking about, I'll try to shed some light.
What is Coderbits? Coderbits is, as the official description says:

Intelligent portfolios for developers and designers

Basically you sign up (Super simple stuff until this point!) and build your profile using data gathered from all your profiles you own, whether it's Github, Dribbble or even the ones with the weird name, like Work for Pie (that is shutting down, if you haven't heard). The community is great (If you build a great tool, great people will come!) and a lot of awesome resources are shared everyday by the community members.

But enough about the awesome community, let's get down to the nitty-gritty (Do we have to? Yes, yes we do!), the API. The Coderbits profile API

provides a simple JSON endpoint for every profile. It is accessed by issuing an HTTPS GET request to the profile URL with .json added to the end.

You can read more about how the API works by heading over to the official page and then the Profile API section. So how do we tackle the issue of using the Coderbits API with PHP?

Disclaimer: For the sake of this tutorial I will keep things as simple and as short as possible. If you have issues with this tutorial please report them in the comments bellow or at hello[at]stefancosma[dot]eu.

Well it's fairly simple (Aren't all things?!). We have some simple and basic functions that can be used to get information from the API and then display the information that we want in a page.

  1. The first function is the one that gathers the info from the Coderbits API.

    // Get the JSON file from coderbits
    function coderbits_profiler_get_json($username) {
        // jSON URL which should be requested
        $json_url = 'https://coderbits.com/' . $username . '.json';
    // Initializing curl
    $ch = curl_init($json_url);
    // Configuring curl options
    $options = array(
        CURLOPT_HTTPHEADER => array('Content-type: application/json')
    // Setting curl options
    curl_setopt_array($ch, $options);
    // Getting results $result = curl_exec($ch); // Getting jSON result string // Save the result into a local file $save_file = file_put_contents(dirname(__FILE__) . '/cache/' . md5($username), $result); // Close request to clear up some resources curl_close($ch); }

This function gathers all the data from the API call, for the specified user, using cURL, and saves it in a cache folder with the name of the file being the username encrypted with md5.

  1. The second function is the most important one of the ones that we are going to use. This function will output the data from the saved file.

    // Get data from JSON file
    function coderbits_profiler_data($type, $subtype = ' ', $username) {
        // The filename path
        $file = dirname(__FILE__) . '/cache/' . md5($username);
    // Check if the file exists
    if (file_exists($file)) {
        // Read the local file for data
        $json_file = file_get_contents($file);
        // Parse the JSON result from the file
        $output = json_decode($json_file, true);
        // Output the requested field
        $return = $output[$type];
        // Check if the field is array
        if (is_array($return)) {
            // If the type is badge do other stuff
            if ($type == 'badges') {
                // Badge limit counter
                $badge_limit_counter = 0;
                // Get all the items in the array
                foreach ($return as $items) {
                    // Each item has multiple arrays
                    foreach($items as $key => $badge) {
                        // Convert key => value arrays into variables
                        $$key = $badge;
                    // Check if the badge has been earned
                    if ($earned && !empty($earned_date)) {
                        // Build the badge
                        $data .= "< a title="' . $name . ' - ' . $description . '" href="' . $link . '" target="_blank">< img alt="badge" src="' . $image_link . '" width="45px" height="45px" />";
                        // Break the loop if we reach 15 entries
                        if (++$badge_limit_counter == 15) break;
                 // Get the badges count
                 badges_count = coderbits_profiler_data('one_bit_badges') + coderbits_profiler_data('eight_bit_badges') + coderbits_profiler_data('sixteen_bit_badges') + coderbits_profiler_data('thirty_two_bit_badges') + coderbits_profiler_data('sixty_four_bit_badges');
                 // Output it
                 $data .= '< a href="https://coderbits.com/' . get_option('coderbits_profiler_username') . '/badges" target="_blank">view all ' . $badges_count . '';
             } elseif ($type == 'accounts') {
                 // Get all the items in the array
                 foreach ($return as $items) {
                     // Each item has multiple arrays
                     foreach($items as $key => $account) {
                         // Convert key => value arrays into variables
                         $$key = $account;
                     $account_image = 'http://coderbits.com/images/' . str_replace(" ","",strtolower($name)) . '32.png';
                     $data .= '< a title="' . $name . '" href="' . $link . '" target="_blank">< img alt="account" src="' . $account_image . '" name="" />';
              } else {
                  // Get all the items in the array
                  foreach ($return as $items) {
                      // Each item has multiple arrays
                      foreach($items as $key => $item){
                          // Check if the key from the loop is the chosen type
                          if ($key == $subtype) {
                              $data .= $item;
                              // Add comma after each element except the last one
                              if (next($items)) $data .= ', ';
        } else {
            // If it's a normal field return it
            $data = $return;
    } else {
        // If the file can't be read fill each value with NULL
        $data = "NULL";
    return $data;

In order to use this function you will need to provide the type of the field you want to output, if the field has a sub-array you have to provided that (it's an optional parameter) and last but not least the username. Those are all the functions we need. To actually use these functions, you need to call* just once* the first function like in the example below:

coderbits_profiler_get_json('bit'); //Because who doesn't love Bit

and after that you can echo each field like in the following example:

echo coderbits_profiler_data('top_area','name','bit');

And now you know how to use the Coderbits API using PHP. Super simple stuff, just like Bit's name!

Useful advice

  1. Call the first function just once, so not to burden your bandwith (it's a lot of data when you make an API call).
  2. Try improving the functions and let me know in the comments bellow.
  3. Read the comments I provided for each line of code.
  4. Experiment!