Skip to content

Commit eff83b0

Browse files
authored
Merge pull request #7 from stoufa06/version-1.8
Version 1.8
2 parents 8a47268 + 14b2019 commit eff83b0

File tree

5 files changed

+222
-25
lines changed

5 files changed

+222
-25
lines changed

README.md

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
- [Table of contents](#table-of-contents)
33
- [Description](#description)
44
- [Installtion](#installtion)
5+
- [Example](#example)
56
- [Usage](#usage)
67
- [Get authorization link](#get-authorization-link)
78
- [Get token credentials](#get-token-credentials)
@@ -19,15 +20,14 @@ PHP library to connect and use garmin wellness api
1920
```
2021
composer require stoufa06/php-garmin-connect-api
2122
```
23+
# Example
24+
25+
Please take a look at [examples](./examples/README.md) folder
2226
# Usage
2327
## Get authorization link
2428
```php
2529
use Stoufa\GarminApi\GarminApi;
2630

27-
/** use this if you are not using utc timezone in your server or your php code **/
28-
$timezone = date_default_timezone_get();
29-
date_default_timezone_set('UTC');
30-
3131
try
3232
{
3333

@@ -52,21 +52,13 @@ catch (\Throwable $th)
5252
{
5353
// catch your exception here
5454
}
55-
finally
56-
{
57-
date_default_timezone_set($timezone);
58-
}
5955

6056
```
6157
## Get token credentials
6258

6359
After the user connects his garmin account successfully it will redirect to callback_uri. "oauth_token" and "oauth_verifier" should be available in $_GET.
6460

6561
```php
66-
/** use this if you are not using utc timezone in your server or your php code **/
67-
$timezone = date_default_timezone_get();
68-
date_default_timezone_set('UTC');
69-
7062
try
7163
{
7264
$config = array(
@@ -88,10 +80,6 @@ catch (\Throwable $th)
8880
{
8981
// catch your exception here
9082
}
91-
finally
92-
{
93-
date_default_timezone_set($timezone);
94-
}
9583
```
9684

9785
## Get Garmin user id

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,8 @@
2727
"platform": {
2828
"php": "7.1"
2929
}
30+
},
31+
"require-dev": {
32+
"vlucas/phpdotenv": "^4.2"
3033
}
3134
}

examples/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Requirements
2+
- php 7.1 or more
3+
- php package [vlucas/phpdotenv](https://github.com/vlucas/phpdotenv) for .env variables
4+
# Usage
5+
- Create .env file in examples folder
6+
- Set env variable `GARMIN_KEY` and `GARMIN_SECRET` and `GARMIN_CALLBACK_URI=http://localhost:8000/example.php`
7+
- Open terminal and go to example folder
8+
- run `php -S localhost:8000`
9+
- open http://localhost:8000/example.php

examples/example.php

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
<?php
2+
require __DIR__.'/../vendor/autoload.php';
3+
4+
use League\OAuth1\Client\Credentials\TokenCredentials;
5+
use Stoufa\GarminApi\GarminApi;
6+
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
7+
$dotenv->load();
8+
session_start();
9+
10+
11+
// unset($_SESSION['identifier']);
12+
// unset($_SESSION['secret']);
13+
14+
if(isset($_GET['oauth_token'], $_GET['oauth_verifier']) || isset($_SESSION['identifier'], $_SESSION['secret'])) {
15+
16+
try
17+
{
18+
$config = array(
19+
'identifier' => getenv('GARMIN_KEY'),
20+
'secret' => getenv('GARMIN_SECRET'),
21+
'callback_uri' => getenv('GARMIN_CALLBACK_URI')
22+
);
23+
24+
$server = new GarminApi($config);
25+
26+
if (isset($_SESSION['identifier'], $_SESSION['secret'])) {
27+
28+
$identifier = $_SESSION['identifier'];
29+
$secret = $_SESSION['secret'];
30+
31+
// recreate tokenCredentials from identifier and secret
32+
$tokenCredentials = new TokenCredentials();
33+
$tokenCredentials->setIdentifier($identifier);
34+
$tokenCredentials->setSecret($secret);
35+
36+
}
37+
else {
38+
// Retrieve the temporary credentials we saved before
39+
$temporaryCredentials = $_SESSION['temporaryCredentials'];
40+
41+
// We will now retrieve token credentials from the server.
42+
$tokenCredentials = $server->getTokenCredentials($temporaryCredentials, $_GET['oauth_token'], $_GET['oauth_verifier']);
43+
44+
// save token identifier in session
45+
$_SESSION['identifier'] = $tokenCredentials->getIdentifier();
46+
$_SESSION['secret'] = $tokenCredentials->getSecret();
47+
}
48+
49+
50+
$uploadStartTimeInSeconds = (new DateTime())->modify('-1 day')->getTimestamp(); // start time in seconds
51+
$uploadEndTimeInSeconds = (new DateTime())->getTimestamp(); // end time in seconds
52+
53+
// Backfill activities before pulling activities (probably you must wait before it fills the summaries)
54+
$params = [
55+
'summaryStartTimeInSeconds' => $uploadStartTimeInSeconds, // time in seconds utc
56+
'summaryEndTimeInSeconds' => $uploadEndTimeInSeconds // time in seconds utc
57+
];
58+
$server->backfillActivitySummary($tokenCredentials, $params);
59+
60+
// User id
61+
$userId = $server->getUserUid($tokenCredentials);
62+
63+
// Activity summaries
64+
$params = [
65+
'uploadStartTimeInSeconds' => $uploadStartTimeInSeconds, // time in seconds utc
66+
'uploadEndTimeInSeconds' => $uploadEndTimeInSeconds // time in seconds utc
67+
];
68+
$summary = $server->getActivitySummary($tokenCredentials, $params);
69+
70+
71+
72+
}
73+
catch (\Throwable $th)
74+
{
75+
// catch your exception here
76+
$error = $th->getMessage();
77+
}
78+
79+
}
80+
else {
81+
try
82+
{
83+
84+
$config = array(
85+
'identifier' => getenv('GARMIN_KEY'),
86+
'secret' => getenv('GARMIN_SECRET'),
87+
'callback_uri' => getenv('GARMIN_CALLBACK_URI')
88+
);
89+
90+
$server = new GarminApi($config);
91+
92+
// Retreive temporary credentials from server
93+
$temporaryCredentials = $server->getTemporaryCredentials();
94+
95+
// Save temporary crendentials in session to use later to retreive authorization token
96+
$_SESSION['temporaryCredentials'] = $temporaryCredentials;
97+
98+
// Get authorization link
99+
$link = $server->getAuthorizationUrl($temporaryCredentials);
100+
}
101+
catch (\Throwable $th)
102+
{
103+
// catch your exception here
104+
$error = $th->getMessage();
105+
}
106+
107+
}
108+
109+
110+
111+
112+
?><!DOCTYPE html>
113+
<html lang="en">
114+
<head>
115+
<meta charset="UTF-8">
116+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
117+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
118+
<title>Document</title>
119+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
120+
</head>
121+
<body>
122+
123+
<div class="container mt-3">
124+
<?php if(isset($error)):?>
125+
<div class="alert alert-danger" role="alert">
126+
<?=$error?>
127+
</div>
128+
<?php elseif(isset($tokenCredentials)):?>
129+
<dl>
130+
<dt>User ID</dt>
131+
<dd><?=$userId?></dd>
132+
133+
<dt>Token</dt>
134+
<dd class="bg-light"><pre><code><?php print_r(($tokenCredentials))?></code></pre></dd>
135+
136+
<dt>Get Identifier</dt>
137+
<dd class="bg-light">$tokenCredentials->getIdentifier() => <?php echo $tokenCredentials->getIdentifier().PHP_EOL;?></dd>
138+
139+
<dt>Get Secrent</dt>
140+
<dd class="bg-light">$tokenCredentials->getSecret() => <?php echo $tokenCredentials->getSecret();?></dd>
141+
142+
143+
<dt>Create new TokenCredentials from identifier and secret</dt>
144+
<dd class="bg-light">
145+
<pre><code>
146+
$identifier = $tokenCredentials->getIdentifier();
147+
$secret = $tokenCredentials->getSecret();
148+
149+
$ts = new TokenCredentials();
150+
$ts->setIdentifier($identifier);
151+
$ts->setSecret($secret);
152+
print_r($ts);
153+
154+
<?php
155+
$identifier = $tokenCredentials->getIdentifier();
156+
$secret = $tokenCredentials->getSecret();
157+
158+
$ts = new TokenCredentials();
159+
$ts->setIdentifier($identifier);
160+
$ts->setSecret($secret);
161+
print_r($ts);
162+
?>
163+
</code></pre>
164+
</dd>
165+
<dt>Summary</dt>
166+
<dd class="bg-light"><pre><?php print_r(json_decode($summary))?></pre></dd>
167+
</dl>
168+
<?php else : ?>
169+
<h2>Click to connect your garmin account</h2>
170+
<a class="btn btn-primary" href="<?=$link?>" role="button">Connect Garmin</a>
171+
<?php endif?>
172+
</div>
173+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-b5kHyXgcpbZJO/tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0" crossorigin="anonymous"></script>
174+
175+
</body>
176+
</html>

src/GarminApi.php

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace Stoufa\GarminApi;
33

4+
use DateTime;
5+
use DateTimeZone;
46
use League\OAuth1\Client\Server\Server;
57
use GuzzleHttp\Exception\BadResponseException;
68
use GuzzleHttp\Exception\GuzzleException;
@@ -149,6 +151,25 @@ protected function protocolHeader(string $method, string $uri, CredentialsInterf
149151
return $this->normalizeProtocolParameters($parameters);
150152
}
151153

154+
/**
155+
* Get the base protocol parameters for an OAuth request.
156+
* Each request builds on these parameters.
157+
*
158+
* @see OAuth 1.0 RFC 5849 Section 3.1
159+
*/
160+
protected function baseProtocolParameters(): array
161+
{
162+
$dateTime = new DateTime('now', new DateTimeZone('UTC'));
163+
164+
return [
165+
'oauth_consumer_key' => $this->clientCredentials->getIdentifier(),
166+
'oauth_nonce' => $this->nonce(),
167+
'oauth_signature_method' => $this->signature->method(),
168+
'oauth_timestamp' => $dateTime->format('U'),
169+
'oauth_version' => '1.0',
170+
];
171+
}
172+
152173
/**
153174
* Get activity summary
154175
*
@@ -294,7 +315,7 @@ public function backfillActivitySummary(TokenCredentials $tokenCredentials, arra
294315
*/
295316
public function backfillDailySummary(TokenCredentials $tokenCredentials, array $params): void
296317
{
297-
return $this->backfill($tokenCredentials, 'dailies', $params);
318+
$this->backfill($tokenCredentials, 'dailies', $params);
298319
}
299320

300321
/**
@@ -307,7 +328,7 @@ public function backfillDailySummary(TokenCredentials $tokenCredentials, array $
307328
*/
308329
public function backfillEpochSummary(TokenCredentials $tokenCredentials, array $params): void
309330
{
310-
return $this->backfill($tokenCredentials, 'epochs', $params);
331+
$this->backfill($tokenCredentials, 'epochs', $params);
311332
}
312333

313334
/**
@@ -320,7 +341,7 @@ public function backfillEpochSummary(TokenCredentials $tokenCredentials, array $
320341
*/
321342
public function backfillActivityDetailsSummary(TokenCredentials $tokenCredentials, array $params): void
322343
{
323-
return $this->backfill($tokenCredentials, 'activityDetails', $params);
344+
$this->backfill($tokenCredentials, 'activityDetails', $params);
324345
}
325346

326347
/**
@@ -333,7 +354,7 @@ public function backfillActivityDetailsSummary(TokenCredentials $tokenCredential
333354
*/
334355
public function backfillSleepSummary(TokenCredentials $tokenCredentials, array $params): void
335356
{
336-
return $this->backfill($tokenCredentials, 'sleep', $params);
357+
$this->backfill($tokenCredentials, 'sleep', $params);
337358
}
338359

339360
/**
@@ -346,7 +367,7 @@ public function backfillSleepSummary(TokenCredentials $tokenCredentials, array $
346367
*/
347368
public function backfillBodyCompositionSummary(TokenCredentials $tokenCredentials, array $params): void
348369
{
349-
return $this->backfill($tokenCredentials, 'bodyComps', $params);
370+
$this->backfill($tokenCredentials, 'bodyComps', $params);
350371
}
351372

352373

@@ -360,7 +381,7 @@ public function backfillBodyCompositionSummary(TokenCredentials $tokenCredential
360381
*/
361382
public function backfillStressDetailsSummary(TokenCredentials $tokenCredentials, array $params): void
362383
{
363-
return $this->backfill($tokenCredentials, 'stressDetails', $params);
384+
$this->backfill($tokenCredentials, 'stressDetails', $params);
364385
}
365386

366387

@@ -374,7 +395,7 @@ public function backfillStressDetailsSummary(TokenCredentials $tokenCredentials,
374395
*/
375396
public function backfillUserMetricsSummary(TokenCredentials $tokenCredentials, array $params): void
376397
{
377-
return $this->backfill($tokenCredentials, 'userMetrics', $params);
398+
$this->backfill($tokenCredentials, 'userMetrics', $params);
378399
}
379400

380401
/**
@@ -387,7 +408,7 @@ public function backfillUserMetricsSummary(TokenCredentials $tokenCredentials, a
387408
*/
388409
public function backfillPulseOxSummary(TokenCredentials $tokenCredentials, array $params): void
389410
{
390-
return $this->backfill($tokenCredentials, 'pulseOx', $params);
411+
$this->backfill($tokenCredentials, 'pulseOx', $params);
391412
}
392413

393414
/**
@@ -400,7 +421,7 @@ public function backfillPulseOxSummary(TokenCredentials $tokenCredentials, array
400421
*/
401422
public function backfillRespirationSummary(TokenCredentials $tokenCredentials, array $params): void
402423
{
403-
return $this->backfill($tokenCredentials, 'respiration', $params);
424+
$this->backfill($tokenCredentials, 'respiration', $params);
404425
}
405426

406427
/**

0 commit comments

Comments
 (0)