PHP code example of danielmcfarland / strava

1. Go to this page and download the library: Download danielmcfarland/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/ */

    

danielmcfarland / strava example snippets


'client_id' => env('CT_STRAVA_CLIENT_ID', '')
'client_secret' => env('CT_STRAVA_SECRET_ID', '')
'redirect_uri' => env('CT_STRAVA_REDIRECT_URI', '')

CodeToad\Strava\StravaServiceProvider::class,

'Strava' => CodeToad\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": ...

use Carbon\Carbon;

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

  // Check if current token has expired
  if(strtotime(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::updateActivityById($token, $activityID, array $updateableActivity);

// Example $updateableActivity
$updateableActivity = [
    'commute' => false,
    'trainer' => false,
    'description' => 'Easier ride than usual',
    'name' => 'Fun ride',
    'type' => 'Ride', // string, instance of ActivityType
    'gear_id' => 'b12345678987654321',
];

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);
  
[  
	['allowance']['15minutes'] => "100",  
	['allowance']['daily'] => "1000",  
	['usage']['15minutes'] => "7",  
	['usage']['daily'] => "352",  
]
  
Strava::getApiLimits();
  
[  
	['15minutes'] => '100',  
	['daily'] => '1000',  
]
  
Strava::getApiAllowanceLimits();
  
[  
	['15minutes'] => '7',  
	['daily'] => '352',  
]
  
Strava::getApiUsageLimits();

$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="CodeToad\Strava\StravaServiceProvider"