2017 © Pedro Peláez
 

library stravaphp

Strava V3 API PHP client with OAuth authentication

image

basvandorst/stravaphp

Strava V3 API PHP client with OAuth authentication

  • Friday, July 13, 2018
  • by basvandorst
  • Repository
  • 26 Watchers
  • 94 Stars
  • 18,307 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 38 Forks
  • 8 Open issues
  • 9 Versions
  • 51 % Grown

The README.md

StravaPHP

Build Status Coverage Status, (*1)

TLDR; Strava V3 API PHP client with OAuth authentication, (*2)

The Strava V3 API is a publicly available interface allowing developers access to the rich Strava dataset. The interface is stable and currently used by the Strava mobile applications. However, changes are occasionally made to improve performance and enhance features. See Strava's changelog for more details., (*3)

In this GitHub repository you can find the PHP implementation of the Strava V3 API. The current version of StravaPHP combines the V3 API with a proper OAuth authentication., (*4)

Getting started

Get your API key

All calls to the Strava API require an access token defining the athlete and application making the call. Any registered Strava user can obtain an access token by first creating an application at labs.strava.com/developers, (*5)

Composer package

Use composer to install this StravaPHP package., (*6)

{
    "require": {
        "basvandorst/stravaphp": "^2.0.0"
    }
}

StravaPHP usage

First, authorisation and authentication

<?php
include 'vendor/autoload.php';

use Strava\API\OAuth;
use Strava\API\Exception;

try {
    $options = [
        'clientId'     => 1234,
        'clientSecret' => 'APP-TOKEN',
        'redirectUri'  => 'http://my-app/callback.php'
    ];
    $oauth = new OAuth($options);

    if (!isset($_GET['code'])) {
        print '<a href="'.$oauth->getAuthorizationUrl([
            // Uncomment required scopes.
            'scope' => [
                'read',
                // 'read_all',
                // 'profile:read_all',
                // 'profile:write',
                // 'activity:read',
                // 'activity:read_all',
                // 'activity:write',
            ]
        ]).'">Connect</a>';
    } else {
        $token = $oauth->getAccessToken('authorization_code', [
            'code' => $_GET['code']
        ]);
        print $token->getToken();
    }
} catch(Exception $e) {
    print $e->getMessage();
}

Then, call your API method!

<?php
include 'vendor/autoload.php';

use Strava\API\Client;
use Strava\API\Exception;
use Strava\API\Service\REST;

try {
    $adapter = new \GuzzleHttp\Client(['base_uri' => 'https://www.strava.com/api/v3/']);
    $service = new REST($token->getToken(), $adapter);  // Define your user token here.
    $client = new Client($service);

    $athlete = $client->getAthlete();
    print_r($athlete);

    $activities = $client->getAthleteActivities();
    print_r($activities);

    $club = $client->getClub(9729);
    print_r($club);
} catch(Exception $e) {
    print $e->getMessage();
}

Class documentation

Strava\API\Factory

Usage

use Strava\API\Factory;

// Configure your app ID, app token and callback uri
$factory = new Factory();
$OAuthClient = $factory->getOAuthClient(1234, 'APP-TOKEN', 'http://my-app/callback.php');

Methods

$factory->getOAuthClient($client_id, $client_secret, $redirect_uri);
$factory->getAPIClient($token);

Strava\API\OAuth

Usage

use Strava\API\OAuth;

// Parameter information: https://strava.github.io/api/v3/oauth/#get-authorize
$options = [
    'clientId'     => 1234,
    'clientSecret' => 'APP-TOKEN',
    'redirectUri'  => 'http://my-app/callback.php'
];
$oauth = new OAuth($options);

// The OAuth authorization procces (1st; let the user approve, 2nd; token exchange with Strava)
if (!isset($_GET['code'])) {
    print '<a href="'.$oauth->getAuthorizationUrl([
        // Uncomment required scopes.
        'scope' => [
            'read',
            // 'read_all',
            // 'profile:read_all',
            // 'profile:write',
            // 'activity:read',
            // 'activity:read_all',
            // 'activity:write',
        ]
    ]).'">Connect</a>';
} else {
    $token = $oauth->getAccessToken('authorization_code', [
        'code' => $_GET['code']
    ]);
    print $token->getToken();
}

Methods

$oauth->getAuthorizationUrl($options = []);
$oauth->getAccessToken($grant = 'authorization_code', $params = []);

Strava\API\Client

Usage

// REST adapter (We use `Guzzle` in this project)
use GuzzleHttp\Client as GuzzleClient;
use Strava\API\Service\REST;
use Strava\API\Client;

$adapter = new GuzzleClient(['base_uri' => 'https://www.strava.com/api/v3/']);
// Service to use (Service\Stub is also available for test purposes)
$service = new REST('RECEIVED-TOKEN', $adapter);

// Receive the athlete!
$client = new Client($service);
$athlete = $client->getAthlete();
print_r($athlete);

Methods

$client->getAthlete($id = null);
$client->getAthleteStats($id);
$client->getAthleteClubs();
$client->getAthleteRoutes($id, $type = null, $after = null, $page = null, $per_page = null);
$client->getAthleteActivities($before = null, $after = null, $page = null, $per_page = null);
$client->getAthleteFriends($id = null, $page = null, $per_page = null);
$client->getAthleteFollowers($id = null, $page = null, $per_page = null);
$client->getAthleteBothFollowing($id, $page = null, $per_page = null);
$client->getAthleteKom($id, $page = null, $per_page = null);
$client->getAthleteZones();
$client->getAthleteStarredSegments($id = null, $page = null, $per_page = null);
$client->updateAthlete($city, $state, $country, $sex, $weight);
$client->getActivityFollowing($before = null, $page = null, $per_page = null); 
$client->getActivity($id, $include_all_efforts = null);
$client->getActivityComments($id, $markdown = null, $page = null, $per_page = null);
$client->getActivityKudos($id, $page = null, $per_page = null);
$client->getActivityPhotos($id, $size = 2048, $photo_sources = 'true');
$client->getActivityZones($id);
$client->getActivityLaps($id);
$client->getActivityUploadStatus($id);
$client->createActivity($name, $type, $start_date_local, $elapsed_time, $description = null, $distance = null);
$client->uploadActivity($file, $activity_type = null, $name = null, $description = null, $private = null, $commute = null, $trainer = null, $data_type = null, $external_id = null);
$client->updateActivity($id, $name = null, $type = null, $private = false, $commute = false, $trainer = false, $gear_id = null, $description = null);
$client->deleteActivity($id);
$client->getGear($id);
$client->getClub($id);
$client->getClubMembers($id, $page = null, $per_page = null);
$client->getClubActivities($id, $page = null, $per_page = null);
$client->getRoute($id);
$client->getRouteAsGPX($id);
$client->getRouteAsTCX($id);
$client->getSegment($id);
$client->getSegmentLeaderboard($id, $gender = null, $age_group = null, $weight_class = null, $following = null, $club_id = null, $date_range = null, $page = null, $per_page = null);
$client->getSegmentExplorer($bounds, $activity_type = 'riding', $min_cat = null, $max_cat = null);
$client->getSegmentEffort($id, $athlete_id = null, $start_date_local = null, $end_date_local = null, $page = null, $per_page = null);
$client->getStreamsActivity($id, $types, $resolution = null, $series_type = 'distance');
$client->getStreamsEffort($id, $types, $resolution = null, $series_type = 'distance');
$client->getStreamsSegment($id, $types, $resolution = null, $series_type = 'distance');
$client->getStreamsRoute($id);

UML diagrams

Class diagram

class, (*7)

Sequence diagram

sequence, (*8)

About StravaPHP

Used libraries

Development

The StravaPHP library was created by Bas van Dorst, software engineer and cyclist enthusiast. And of course, special thanks to all contributors, (*9)

Contributing

All issues and pull requests should be filled on the basvandorst/StravaPHP repository., (*10)

License

The StravaPHP library is open-source software licensed under MIT license., (*11)

The Versions

13/07 2018

dev-master

9999999-dev

Strava V3 API PHP client with OAuth authentication

  Sources   Download

MIT

The Requires

 

The Development Requires

by Bas van Dorst

api php oauth strava stravaphp

13/07 2018

1.2.0

1.2.0.0

Strava V3 API PHP client with OAuth authentication

  Sources   Download

MIT

The Requires

 

The Development Requires

by Bas van Dorst

api php oauth strava stravaphp

21/10 2017

1.1.0

1.1.0.0

Strava V3 API PHP client with OAuth authentication

  Sources   Download

MIT

The Requires

 

The Development Requires

by Bas van Dorst

api php oauth strava stravaphp

19/09 2016

1.0.2

1.0.2.0

Strava V3 API PHP client with OAuth authentication

  Sources   Download

MIT

The Requires

 

by Bas van Dorst

api php oauth strava stravaphp

19/09 2016

dev-qligier-pr1

dev-qligier-pr1

Strava V3 API PHP client with OAuth authentication

  Sources   Download

MIT

The Requires

 

by Bas van Dorst

api php oauth strava stravaphp

23/02 2015

1.0.1

1.0.1.0

Strava V3 API PHP client with OAuth authentication

  Sources   Download

MIT

The Requires

 

by Bas van Dorst

api php oauth strava stravaphp

29/12 2014

1.0.0

1.0.0.0

Strava V3 API PHP client with OAuth authentication

  Sources   Download

MIT

The Requires

 

by Bas van Dorst

api php oauth strava stravaphp

25/10 2014

dev-scrutinizer-patch-2

dev-scrutinizer-patch-2

test

  Sources   Download

MIT

The Requires

 

by Bas van Dorst

18/10 2014

dev-scrutinizer-patch-1

dev-scrutinizer-patch-1

test

  Sources   Download

MIT

The Requires

 

by Bas van Dorst