1. Go to this page and download the library: Download basvandorst/stravaphp 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/ */
basvandorst / stravaphp example snippets
include 'vendor/autoload.php';
use Strava\API\OAuth;
use Strava\API\Exception;
try {
$options = [
'clientId' => 1234,
'clientSecret' => 'APP-TOKEN',
'redirectUri' => 'http://my-app/callback.php'
];
$oauth = new OAuth($options);
if (!isset($_GET['code'])) {
print '<a href="'.$oauth->getAuthorizationUrl([
// Uncomment 'code' => $_GET['code']
]);
print $token->getToken();
}
} catch(Exception $e) {
print $e->getMessage();
}
Strava\API\Client;
use Strava\API\Exception;
use Strava\API\Service\REST;
try {
$adapter = new \GuzzleHttp\Client(['base_uri' => 'https://www.strava.com/api/v3/']);
$service = new REST($token->getToken(), $adapter); // Define your user token here.
$client = new Client($service);
$athlete = $client->getAthlete();
print_r($athlete);
$activities = $client->getAthleteActivities();
print_r($activities);
$club = $client->getClub(9729);
print_r($club);
} catch(Exception $e) {
print $e->getMessage();
}
use Strava\API\Factory;
// Configure your app ID, app token and callback uri
$factory = new Factory();
$OAuthClient = $factory->getOAuthClient(1234, 'APP-TOKEN', 'http://my-app/callback.php');
// REST adapter (We use `Guzzle` in this project)
use GuzzleHttp\Client as GuzzleClient;
use Strava\API\Service\REST;
use Strava\API\Client;
$adapter = new GuzzleClient(['base_uri' => 'https://www.strava.com/api/v3/']);
// Service to use (Service\Stub is also available for test purposes)
$service = new REST('RECEIVED-TOKEN', $adapter);
// Receive the athlete!
$client = new Client($service);
$athlete = $client->getAthlete();
print_r($athlete);
Strava\API\Client;
use Strava\API\Service\REST;
use GuzzleHttp\Client as GuzzleClient;
// Create API client
$adapter = new GuzzleClient(['base_uri' => 'https://www.strava.com/api/v3/']);
$service = new REST('YOUR_ACCESS_TOKEN', $adapter);
$client = new Client($service);
// Your Strava app credentials
$clientId = 12345;
$clientSecret = 'your_client_secret';
$callbackUrl = 'https://yourdomain.com/webhook-endpoint.php';
$verifyToken = 'your_random_verify_token';
try {
// Create a webhook subscription
$subscription = $client->createWebhookSubscription(
$clientId,
$clientSecret,
$callbackUrl,
$verifyToken
);
echo "Webhook subscription created: " . $subscription['id'] . "\n";
// List existing subscriptions
$subscriptions = $client->listWebhookSubscriptions($clientId, $clientSecret);
echo "Active subscriptions: " . count($subscriptions) . "\n";
// Delete a subscription
$client->deleteWebhookSubscription($clientId, $clientSecret, $subscription['id']);
echo "Subscription deleted\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
// webhook-endpoint.php
Handle subscription challenge (when creating webhook)
$verifyToken = 'your_random_verify_token';
$challengeResult = Webhook::handleSubscriptionChallenge($verifyToken);
if ($challengeResult['success']) {
// Send challenge response to Strava
Webhook::sendChallengeResponse($challengeResult['challenge']);
}
// Process incoming webhook events
$eventResult = Webhook::processEvent();
if ($eventResult['success']) {
$event = $eventResult['event'];
// Handle different event types
switch (Webhook::getEventType($event)) {
case 'activity.create':
echo "New activity: " . $event['object_id'] . "\n";
// Process new activity
break;
case 'activity.update':
echo "Updated activity: " . $event['object_id'] . "\n";
// Process activity update
break;
case 'activity.delete':
echo "Deleted activity: " . $event['object_id'] . "\n";
// Process activity deletion
break;
case 'athlete.update':
echo "Updated athlete: " . $event['object_id'] . "\n";
// Process athlete update
break;
}
// Send success response
http_response_code(200);
echo json_encode(['status' => 'success']);
} else {
// Handle error
http_response_code(400);
echo json_encode(['error' => $eventResult['error']]);
}
// Get event type (e.g., 'activity.create')
$eventType = Webhook::getEventType($event);
// Check if event is for specific object type
$isActivity = Webhook::isObjectType($event, 'activity');
$isAthlete = Webhook::isObjectType($event, 'athlete');
// Check if event is specific aspect type
$isCreate = Webhook::isAspectType($event, 'create');
$isUpdate = Webhook::isAspectType($event, 'update');
$isDelete = Webhook::isAspectType($event, 'delete');
// Verify webhook signature (if using signature verification)
$isValid = Webhook::verifySignature($payload, $signature, $secret);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.