@@ -211,8 +211,152 @@ $client->getStreamsActivity($id, $types, $resolution = null, $series_type = 'dis
211211$client->getStreamsEffort($id, $types, $resolution = null, $series_type = 'distance');
212212$client->getStreamsSegment($id, $types, $resolution = null, $series_type = 'distance');
213213$client->getStreamsRoute($id);
214+ $client->createWebhookSubscription($clientId, $clientSecret, $callbackUrl, $verifyToken);
215+ $client->listWebhookSubscriptions($clientId, $clientSecret);
216+ $client->deleteWebhookSubscription($clientId, $clientSecret, $subscriptionId);
214217```
215218
219+ ## Webhook Integration
220+
221+ StravaPHP now includes comprehensive webhook support for real-time event notifications. This allows you to receive instant notifications when activities are created, updated, or deleted.
222+
223+ ### Webhook Subscription Management
224+
225+ ``` php
226+ <?php
227+ include 'vendor/autoload.php';
228+
229+ use Strava\API\Client;
230+ use Strava\API\Service\REST;
231+ use GuzzleHttp\Client as GuzzleClient;
232+
233+ // Create API client
234+ $adapter = new GuzzleClient(['base_uri' => 'https://www.strava.com/api/v3/']);
235+ $service = new REST('YOUR_ACCESS_TOKEN', $adapter);
236+ $client = new Client($service);
237+
238+ // Your Strava app credentials
239+ $clientId = 12345;
240+ $clientSecret = 'your_client_secret';
241+ $callbackUrl = 'https://yourdomain.com/webhook-endpoint.php';
242+ $verifyToken = 'your_random_verify_token';
243+
244+ try {
245+ // Create a webhook subscription
246+ $subscription = $client->createWebhookSubscription(
247+ $clientId,
248+ $clientSecret,
249+ $callbackUrl,
250+ $verifyToken
251+ );
252+
253+ echo "Webhook subscription created: " . $subscription['id'] . "\n";
254+
255+ // List existing subscriptions
256+ $subscriptions = $client->listWebhookSubscriptions($clientId, $clientSecret);
257+ echo "Active subscriptions: " . count($subscriptions) . "\n";
258+
259+ // Delete a subscription
260+ $client->deleteWebhookSubscription($clientId, $clientSecret, $subscription['id']);
261+ echo "Subscription deleted\n";
262+
263+ } catch (Exception $e) {
264+ echo "Error: " . $e->getMessage() . "\n";
265+ }
266+ ```
267+
268+ ### Webhook Event Handling
269+
270+ ``` php
271+ <?php
272+ // webhook-endpoint.php
273+ include 'vendor/autoload.php';
274+
275+ use Strava\API\Webhook;
276+
277+ // Handle subscription challenge (when creating webhook)
278+ $verifyToken = 'your_random_verify_token';
279+ $challengeResult = Webhook::handleSubscriptionChallenge($verifyToken);
280+
281+ if ($challengeResult['success']) {
282+ // Send challenge response to Strava
283+ Webhook::sendChallengeResponse($challengeResult['challenge']);
284+ }
285+
286+ // Process incoming webhook events
287+ $eventResult = Webhook::processEvent();
288+
289+ if ($eventResult['success']) {
290+ $event = $eventResult['event'];
291+
292+ // Handle different event types
293+ switch (Webhook::getEventType($event)) {
294+ case 'activity.create':
295+ echo "New activity: " . $event['object_id'] . "\n";
296+ // Process new activity
297+ break;
298+
299+ case 'activity.update':
300+ echo "Updated activity: " . $event['object_id'] . "\n";
301+ // Process activity update
302+ break;
303+
304+ case 'activity.delete':
305+ echo "Deleted activity: " . $event['object_id'] . "\n";
306+ // Process activity deletion
307+ break;
308+
309+ case 'athlete.update':
310+ echo "Updated athlete: " . $event['object_id'] . "\n";
311+ // Process athlete update
312+ break;
313+ }
314+
315+ // Send success response
316+ http_response_code(200);
317+ echo json_encode(['status' => 'success']);
318+ } else {
319+ // Handle error
320+ http_response_code(400);
321+ echo json_encode(['error' => $eventResult['error']]);
322+ }
323+ ```
324+
325+ ### Webhook Helper Methods
326+
327+ The ` Webhook ` class provides several utility methods:
328+
329+ ``` php
330+ // Get event type (e.g., 'activity.create')
331+ $eventType = Webhook::getEventType($event);
332+
333+ // Check if event is for specific object type
334+ $isActivity = Webhook::isObjectType($event, 'activity');
335+ $isAthlete = Webhook::isObjectType($event, 'athlete');
336+
337+ // Check if event is specific aspect type
338+ $isCreate = Webhook::isAspectType($event, 'create');
339+ $isUpdate = Webhook::isAspectType($event, 'update');
340+ $isDelete = Webhook::isAspectType($event, 'delete');
341+
342+ // Verify webhook signature (if using signature verification)
343+ $isValid = Webhook::verifySignature($payload, $signature, $secret);
344+ ```
345+
346+ ### Webhook Events
347+
348+ Strava webhooks support the following event types:
349+
350+ - ** Activity Events:**
351+ - ` activity.create ` - New activity created
352+ - ` activity.update ` - Activity updated
353+ - ` activity.delete ` - Activity deleted
354+
355+ - ** Athlete Events:**
356+ - ` athlete.update ` - Athlete profile updated
357+
358+ For more information about webhook events, see the [ Strava Webhook Documentation] ( https://developers.strava.com/docs/webhooks/ ) .
359+
216360## UML diagrams
217361### Class diagram
218362![ class] ( https://cloud.githubusercontent.com/assets/1196963/4705696/764cd4e2-587e-11e4-8c9f-d265255ee0a2.png )
0 commit comments