Back

Step by Step Guide to Building a Runkeeper API Integration in PHP

Aug 18, 20246 minute read

Introduction

Hey there, fellow code wrangler! Ready to dive into the world of fitness data? We're about to embark on a journey to integrate the Runkeeper API into your PHP project. Buckle up, because by the end of this guide, you'll be pulling fitness stats like a pro.

Prerequisites

Before we hit the ground running, make sure you've got:

  • A PHP environment that's up and snuff
  • Your Runkeeper API credentials (if you don't have 'em, go grab 'em!)
  • cURL extension enabled (we'll be making some HTTP requests)

Got all that? Great! Let's move on to the fun stuff.

Authentication

First things first, we need to get that sweet, sweet access token. We'll be using OAuth 2.0, so let's set that up:

$client_id = 'YOUR_CLIENT_ID'; $client_secret = 'YOUR_CLIENT_SECRET'; $redirect_uri = 'YOUR_REDIRECT_URI'; $auth_url = "https://runkeeper.com/apps/authorize?client_id={$client_id}&response_type=code&redirect_uri={$redirect_uri}"; // Redirect the user to $auth_url, then handle the callback to get the code $code = $_GET['code']; $token_url = 'https://runkeeper.com/apps/token'; $data = [ 'grant_type' => 'authorization_code', 'code' => $code, 'client_id' => $client_id, 'client_secret' => $client_secret, 'redirect_uri' => $redirect_uri ]; $ch = curl_init($token_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); $response = curl_exec($ch); $token_data = json_decode($response, true); $access_token = $token_data['access_token'];

Making API Requests

Now that we're authenticated, let's set up our HTTP client to make some requests:

function make_api_request($endpoint, $access_token, $method = 'GET', $data = null) { $ch = curl_init("https://api.runkeeper.com{$endpoint}"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Authorization: Bearer {$access_token}", 'Content-Type: application/json' ]); if ($method === 'POST') { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); } $response = curl_exec($ch); curl_close($ch); return json_decode($response, true); }

Retrieving User Data

Let's grab some user data, shall we?

$user_info = make_api_request('/user', $access_token); $activity_summary = make_api_request('/fitnessActivities', $access_token); echo "Welcome, {$user_info['name']}! You've completed {$activity_summary['size']} activities.";

Working with Activities

Time to dig into those activities:

$activities = make_api_request('/fitnessActivities', $access_token); foreach ($activities['items'] as $activity) { $activity_details = make_api_request($activity['uri'], $access_token); echo "Activity on {$activity_details['start_time']}: {$activity_details['type']} - {$activity_details['total_distance']} meters\n"; }

Posting New Activities

Feeling creative? Let's post a new activity:

$new_activity = [ 'type' => 'Running', 'start_time' => '2023-06-01T12:00:00Z', 'duration' => 1800, 'total_distance' => 5000, 'notes' => 'Great run in the park!' ]; $result = make_api_request('/fitnessActivities', $access_token, 'POST', $new_activity); echo $result['uri'] ? "Activity posted successfully!" : "Oops, something went wrong.";

Handling Errors and Rate Limiting

Don't forget to play nice with the API. Here's a quick way to handle rate limiting:

function make_api_request_with_rate_limit($endpoint, $access_token, $method = 'GET', $data = null) { $response = make_api_request($endpoint, $access_token, $method, $data); if (isset($response['errors']) && $response['errors'][0]['type'] === 'rate_limit_exceeded') { sleep(60); // Wait for a minute return make_api_request($endpoint, $access_token, $method, $data); } return $response; }

Data Processing and Storage

Now that we've got our data, let's do something with it:

$activities = make_api_request_with_rate_limit('/fitnessActivities', $access_token); $total_distance = 0; foreach ($activities['items'] as $activity) { $total_distance += $activity['total_distance']; } echo "Total distance covered: {$total_distance} meters"; // If you want to store this in a database, now's your chance!

Conclusion

And there you have it! You've just built a Runkeeper API integration in PHP. You're now armed with the power to fetch, post, and analyze fitness data like a champ. Remember, this is just scratching the surface - there's a whole world of possibilities out there with the Runkeeper API.

Keep coding, keep running, and most importantly, keep pushing your limits!