PHP code example of lachlanhickey / laravel-strava
1. Go to this page and download the library: Download lachlanhickey/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/ */
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
}
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'));
}
}