0%

Strava API

A very useful Strava PHP library and it’s very easy to use:

1
2
git clone https://github.com/basvandorst/StravaPHP.git
composer install
  • A sample PHP page:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    <?php
    include '../vendor/autoload.php';

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

    try {
    // **Change** blow parameters with your own APP value
    $options = [
    'clientId' => 888888,
    'clientSecret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    // **Change** here to redirect to the same page to post and receive the token.
    'redirectUri' => 'http://mengc06.cpanel.unitec.ac.nz/sportanalysis/public/'
    ];
    $oauth = new OAuth($options);

    if (!isset($_GET['code'])) {
    print '<a href="'.$oauth->getAuthorizationUrl([
    // Uncomment required scopes.
    'scope' => [
    'public',
    // 'write',
    // 'view_private',
    ]
    ]).'"><img alt="Connect with Strava" src="/sportanalysis/public/img/ConnectToStrava.png"></a>';
    } else {
    $token = $oauth->getAccessToken('authorization_code', [
    'code' => $_GET['code']
    ]);
    echo '<h4>';
    print $token->getToken();
    echo '</h4>';

    try {
    $adapter = new Pest('https://www.strava.com/api/v3');
    $service = new REST($token, $adapter); // Define your user token here.
    $client = new Client($service);

    // print out whatever you want.
    echo "<br />";
    $athlete = $client->getAthlete();
    print_r($athlete);
    echo "<br />";
    $activities = $client->getAthleteActivities();
    print_r($activities);
    echo "<br />";
    $club = $client->getClub(9729);
    print_r($club);
    } catch(Exception $e) {
    print $e->getMessage();
    }
    }
    } catch(Exception $e) {
    print $e->getMessage();
    }
    ?>
  • The OAuth Flow:

    • Get method redirect customer to strava with client_id, redirect_uri, etc, but not client_secret
    • customer is asked to logon Strava
    • customer is asked to grant data access to our APP
    • on success,strava redirect back to redirect_uri and exchange the temporary authorization code for an access token, using our client ID and client_secret [POST to https://www.strava.com/oauth/token]
    • if success our server will receive a customer token
    • then we can use this token in Strava API to collect customer data.