|
1 | | -# php-garmin-connect-api |
| 1 | +# Table of contents |
| 2 | +- [Table of contents](#table-of-contents) |
| 3 | +- [Description](#description) |
| 4 | +- [Installtion](#installtion) |
| 5 | +- [Usage](#usage) |
| 6 | + - [Get authorization link](#get-authorization-link) |
| 7 | + - [Get token credentials](#get-token-credentials) |
| 8 | + - [Get Garmin user id](#get-garmin-user-id) |
| 9 | + - [Backfill activities](#backfill-activities) |
| 10 | + - [Deregistration](#deregistration) |
| 11 | + - [Avalaible methods (for the moment)](#avalaible-methods-for-the-moment) |
| 12 | + - [Get summary activities](#get-summary-activities) |
| 13 | + - [Backfill activities](#backfill-activities-1) |
| 14 | + |
| 15 | +# Description |
2 | 16 | PHP library to connect and use garmin wellness api |
| 17 | + |
| 18 | +# Installtion |
| 19 | +``` |
| 20 | +composer require stoufa06/php-garmin-connect-api |
| 21 | +``` |
| 22 | +# Usage |
| 23 | +## Get authorization link |
| 24 | +```php |
| 25 | +use Stoufa\GarminApi\GarminApi; |
| 26 | + |
| 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 | + |
| 31 | +try |
| 32 | +{ |
| 33 | + |
| 34 | + $config = array( |
| 35 | + 'identifier' => getenv('GARMIN_KEY'), |
| 36 | + 'secret' => getenv('GARMIN_SECRET'), |
| 37 | + 'callback_uri' => getenv('GARMIN_CALLBACK_URI') |
| 38 | + ); |
| 39 | + |
| 40 | + $server = new GarminApi($config); |
| 41 | + |
| 42 | + // Retreive temporary credentials from server |
| 43 | + $temporaryCredentials = $server->getTemporaryCredentials(); |
| 44 | + |
| 45 | + // Save temporary crendentials in session to use later to retreive authorization token |
| 46 | + $_SESSION['temporaryCredentials'] = $temporaryCredentials; |
| 47 | + |
| 48 | + // Get authorization link |
| 49 | + $link = $server->getAuthorizationUrl($temporaryCredentials); |
| 50 | +} |
| 51 | +catch (\Throwable $th) |
| 52 | +{ |
| 53 | + // catch your exception here |
| 54 | +} |
| 55 | +finally |
| 56 | +{ |
| 57 | + date_default_timezone_set($timezone); |
| 58 | +} |
| 59 | + |
| 60 | +``` |
| 61 | +## Get token credentials |
| 62 | + |
| 63 | +After the user connects his garmin account successfully it will redirect to callback_uri. "oauth_token" and "oauth_verifier" should be available in $_GET. |
| 64 | + |
| 65 | +```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 | + |
| 70 | +try |
| 71 | +{ |
| 72 | + $config = array( |
| 73 | + 'identifier' => getenv('GARMIN_KEY'), |
| 74 | + 'secret' => getenv('GARMIN_SECRET'), |
| 75 | + 'callback_uri' => getenv('GARMIN_CALLBACK_URI') |
| 76 | + ); |
| 77 | + |
| 78 | + $server = new GarminApi($config); |
| 79 | + |
| 80 | + // Retrieve the temporary credentials we saved before |
| 81 | + $temporaryCredentials = $_SESSION['temporaryCredentials']; |
| 82 | + |
| 83 | + // We will now retrieve token credentials from the server. |
| 84 | + $tokenCredentials = $server->getTokenCredentials($temporaryCredentials, $_GET['oauth_token'], $_GET['oauth_verifier']); |
| 85 | + |
| 86 | +} |
| 87 | +catch (\Throwable $th) |
| 88 | +{ |
| 89 | + // catch your exception here |
| 90 | +} |
| 91 | +finally |
| 92 | +{ |
| 93 | + date_default_timezone_set($timezone); |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +## Get Garmin user id |
| 98 | + |
| 99 | +```php |
| 100 | +$userId = $server->getUserUid($tokenCredentials); |
| 101 | +``` |
| 102 | + |
| 103 | +## Backfill activities |
| 104 | + |
| 105 | +When you connect garmin account and get token credentials first time, you won't be able to get previous activities because garmin does not give you activities older than your token credentials. Instead you need to use backfill method to fullfull your token with previous activities (no more than one month). |
| 106 | + |
| 107 | + |
| 108 | +```php |
| 109 | +// backfill activities for last 7 days ago |
| 110 | +$params = [ |
| 111 | + 'summaryStartTimeInSeconds' => strtotime("-7 days", time()), |
| 112 | + 'summaryEndTimeInSeconds' => time() |
| 113 | +]; |
| 114 | +$server->backfillActivitySummary($tokenCredentials, $params); |
| 115 | +``` |
| 116 | +## Deregistration |
| 117 | +```php |
| 118 | +$server->deleteUserAccessToken($tokenCredentials); |
| 119 | +``` |
| 120 | + |
| 121 | +## Avalaible methods (for the moment) |
| 122 | + |
| 123 | +### Get summary activities |
| 124 | +```php |
| 125 | +$params = [ |
| 126 | + 'uploadStartTimeInSeconds' => 1598814036, // time in seconds utc |
| 127 | + 'uploadEndTimeInSeconds' => 1598900435 // time in seconds utc |
| 128 | +]; |
| 129 | + |
| 130 | +// Activity summaries |
| 131 | +$server->getActivitySummary($tokenCredentials, $params); |
| 132 | + |
| 133 | +// Manually activity summaries |
| 134 | +$server->getManuallyActivitySummary($tokenCredentials, $params); |
| 135 | + |
| 136 | +// Activity details summaries |
| 137 | +$server->getActivityDetailsSummary($tokenCredentials, $params); |
| 138 | +``` |
| 139 | + |
| 140 | + |
| 141 | +### Backfill activities |
| 142 | +```php |
| 143 | +// For backfill params can be with upload start time |
| 144 | +$params = [ |
| 145 | + 'uploadStartTimeInSeconds' => 1598814036, // time in seconds utc |
| 146 | + 'uploadEndTimeInSeconds' => 1598900435 // time in seconds utc |
| 147 | +]; |
| 148 | +// or with summary start time |
| 149 | +$params = [ |
| 150 | + 'summaryStartTimeInSeconds' => 1598814036, // time in seconds utc |
| 151 | + 'summaryEndTimeInSeconds' => 1598900435 // time in seconds utc |
| 152 | +]; |
| 153 | + |
| 154 | +// Backfill activity summaries |
| 155 | +$server->backfillActivitySummary($tokenCredentials, $params); |
| 156 | + |
| 157 | +// Backfill daily activity summaries |
| 158 | +$server->backfillDailySummary($tokenCredentials, $params); |
| 159 | + |
| 160 | +// Backfill epoch summaries |
| 161 | +$server->backfillEpochSummary($tokenCredentials, $params); |
| 162 | + |
| 163 | +// Backfill activity details summaries |
| 164 | +$server->backfillActivityDetailsSummary($tokenCredentials, $params); |
| 165 | + |
| 166 | +// Backfill sleep summaries |
| 167 | +$server->backfillSleepSummary($tokenCredentials, $params); |
| 168 | + |
| 169 | +// Backfill body composition summaries |
| 170 | +$server->backfillBodyCompositionSummary($tokenCredentials, $params); |
| 171 | + |
| 172 | +// Backfill stress details summaries |
| 173 | +$server->backfillStressDetailsSummary($tokenCredentials, $params); |
| 174 | + |
| 175 | +// Backfill user metrics summaries |
| 176 | +$server->backfillUserMetricsSummary($tokenCredentials, $params); |
| 177 | + |
| 178 | +// Backfill pulse ox summaries |
| 179 | +$server->backfillPulseOxSummary($tokenCredentials, $params); |
| 180 | + |
| 181 | +// Backfill respiration summaries |
| 182 | +$server->backfillRespirationSummary($tokenCredentials, $params); |
| 183 | +``` |
0 commit comments