Strava API in PHP

2,687 views
Skip to first unread message

Patrick Lazalita

unread,
Mar 3, 2014, 12:47:20 AM3/3/14
to strav...@googlegroups.com
Hi,


Is anybody already use the strava api through php?
I can't get it done using this one: https://github.com/iamstuartwilson/strava

Can anyone shed some light on how to go throught it.
 
Thank you very much.

Bas Vredeling

unread,
Jun 8, 2014, 12:00:00 PM6/8/14
to strav...@googlegroups.com
I just tried it as connection library for a little Drupal plugin I'm writing. I can confirm it works.
Some stuff to doublecheck while developing:
  1. create a new application at https://www.strava.com/settings/api
  2. save the client id / client secret code (and optionally the access token) in your client application
  3. if you're using a local develop environment, make sure you set up the authentication domain to reflect your local settings in https://www.strava.com/settings/api. For instance: http://localhost.local
  4. skip the whole part about authenticationUrl() and tokenExchange() if you're using the generated token from https://www.strava.com/settings/api anyway
  5. try debugging the api calls directly by just opening the direct request urls and manually inspecting the response

Koen de Vries

unread,
Jun 10, 2014, 2:49:11 AM6/10/14
to strav...@googlegroups.com
Hi Patrick,

I'm using it on http://www.padvark.nl, works great.

Some sample code:

public function oauth($redirurl){
$athlete = null;
if(isset($_GET['code']) and !is_null($_GET['code'])){
$post_data = array(
'client_id' => '<yourclientid>',
'client_secret' => '<yourclientsecret>',
'code' => $_GET['code']
);
$options = array(
'http' => array(
'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
'method'  => 'POST',
'content' => http_build_query($post_data),
),
);
$context  = stream_context_create($options);
$result = file_get_contents('https://www.strava.com/oauth/token', false, $context);
$token = json_decode($result)->access_token;
if($token != null){
$_SESSION['stravatoken'] = $token;
header('Location: '.$redirurl);
}
}
}
public function getAthlete(){
return json_decode(file_get_contents('http://www.strava.com/api/v3/athlete?access_token='.$_SESSION['stravatoken'];
}


Op maandag 3 maart 2014 06:47:20 UTC+1 schreef Patrick Lazalita:
Reply all
Reply to author
Forward
0 new messages