PHP code example of deniztezcan / laravel-strava

1. Go to this page and download the library: Download deniztezcan/laravel-strava library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

deniztezcan / laravel-strava example snippets


'client_id' => env('STRAVA_CLIENT_ID', '')
'client_secret' => env('STRAVA_SECRET_ID', '')
'redirect_uri' => env('STRAVA_REDIRECT_URI', '')

DenizTezcan\Strava\StravaServiceProvider::class,

'Strava' => DenizTezcan\Strava\StravaFacade::class,

use Strava;

class MyController extends Controller
{
  // Controller functions here...
}

public function stravaAuth()
{
  return Strava::authenticate($scope='read_all,profile:read_all,activity:read_all');
}

public function getToken(Request $request)
{
  $token = Strava::token($request->code);

  // Store $token->access_token & $token->refresh_token in database
}

"token_type": "Bearer"
"expires_at": 1555165838
"expires_in": 21600 // 6 Hours
"refresh_token": "671129e56b1ce64d7e0c7e594cb6522b239464e1"
"access_token": "e105062b153da39f81a439b90b23357c741a40a0"
"athlete": ...

public function myControllerFunction(Request $request)
{
  // Get the user
  $user = User::find($request->id);

  // Check if current token has expired
  if(Carbon::now() > $user->expires_at)
  {
    // Token has expired, generate new tokens using the currently stored user refresh token
    $refresh = Strava::refreshToken($user->refresh_token);

    // Update the users tokens
    User::where('id', $request->id)->update([
      'access_token' => $refresh->access_token,
      'refresh_token' => $refresh->refresh_token
    ]);

    // Call Strava Athlete Method with newly updated access token.
    $athlete = Strava::athlete($user->access_token);

    // Return $athlete array to view
    return view('strava.athlete')->with(compact('athlete'));

  }else{

    // Token has not yet expired, Call Strava Athlete Method
    $athlete = Strava::athlete($user->access_token);

    // Return $athlete array to view
    return view('strava.athlete')->with(compact('athlete'));

  }

}

Strava::unauthenticate($token);

Strava::athlete($token);

Strava::activities($token, $page, $perPage, $before, $after);

Strava::activity($token, $activityID);

// $keys is a string array containing tivityStream($token, $activityID, $keys = '', $keyByType = true);

Strava::activityComments($token, $activityID, $page, $perPage);

Strava::activityKudos($token, $activityID, $page, $perPage);

Strava::activityLaps($token, $activityID);

Strava::activityZones($token, $activityID);

Strava::athleteZones($token);

Strava::athleteStats($token, $athleteID, $page, $perPage);

Strava::club($token, $clubID);

Strava::clubMembers($token, $clubID, $page, $perPage);

Strava::clubActivities($token, $clubID, $page, $perPage);

Strava::clubAdmins($token, $clubID, $page, $perPage);

Strava::athleteClubs($token, $page, $perPage);

Strava::gear($token, $gearID);

Strava::route($token, $routeID);

Strava::athleteRoutes($token, $athleteID, $page, $perPage);

Strava::segment($token, $segmentID);

Strava::segmentEffort($token, $segmentID);

Strava::starredSegments($token, $page, $perPage);

$token        = String
$activityID   = integer
$athleteID    = integer
$clubID       = integer
$gearID       = integer
$routeID      = integer
$segmentID    = integer
$page         = integer
$perPage      = integer
$before       = integer (timestamp)
$after        = integer (timestamp)
shell
php artisan vendor:publish --provider="DenizTezcan\Strava\StravaServiceProvider"
text
STRAVA_CLIENT_ID=ADD-STRAVA-CLIENT-ID-HERE
STRAVA_SECRET_ID=ADD-STRAVA-SECRET-HERE
STRAVA_REDIRECT_URI=ADD-STRAVA-REDIRECT-URI-HERE