-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGarminConnectClient.php
More file actions
190 lines (166 loc) · 6.55 KB
/
GarminConnectClient.php
File metadata and controls
190 lines (166 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
namespace Causal\F2GC;
class GarminConnectClient extends AbstractClient
{
/**
* @return bool
*/
public function connect(bool $force = false): bool
{
if ($this->token !== null && !$force) {
return true;
}
$parameters = [
'service' => 'https://connect.garmin.com/modern/',
'webhost' => 'https://connect.garmin.com',
'source' => 'https://connect.garmin.com/en-US/signin',
'redirectAfterAccountLoginUrl' => 'https://connect.garmin.com/modern/',
'redirectAfterAccountCreationUrl' => 'https://connect.garmin.com/modern/',
'gauthHost' => 'https://sso.garmin.com/sso',
'locale' => 'en-US',
'id' => 'gauth-widget',
'privateStatementUrl' => '//connect.garmin.com/en-US/privacy/',
'clientId' => 'GarminConnect',
'rememberMeShown' => 'true',
'rememberMeChecked' => 'false',
'createAccountShown' => 'true',
'openCreateAccount' => 'false',
'displayNameShown' => 'false',
'consumeServiceTicket' => 'false',
'initialFocus' => 'true',
'embedWidget' => 'false',
'generateExtraServiceTicket' => 'false',
'generateNoServiceTicket' => 'false',
'globalOptInShown' => 'true',
'globalOptInChecked' => 'false',
'mobile' => 'false',
'connectLegalTerms' => 'true',
'locationPromptShown' => 'true',
];
$loginUrl = 'https://sso.garmin.com/sso/login?' . http_build_query($parameters);
$fields = [];
$fields['username'] = $this->username;
$fields['password'] = $this->password;
$fields['embed'] = 'false';
$fields['rememberme'] = 'on';
// This will update internal cookies
$html = $this->doPost($loginUrl, $fields);
if (preg_match('/var response_url\\s*=.*\\?ticket=([^"]+)"/', $html, $matches)) {
// We need this to get an actual session
$this->doGet('/modern/', ['ticket' => $matches[1]]);
return true;
}
return false;
}
/**
* Returns the weight data points.
*
* @return array
*/
public function getWeightValues() : array
{
$oneYearAgo = strtotime('-1 year') * 1000; // 1000 because the Garmin Connect API is Java-based
$now = time() * 1000;
$weightValues = $this->doGet('/modern/proxy/userprofile-service/userprofile/personal-information/weightWithOutbound/filterByDay', [
'from' => $oneYearAgo,
'until' => $now,
]);
return $weightValues;
}
/**
* Adds a weight data point.
*
* @param string $date Format YYYY-MM-DD
* @param float $weight Weight in kg
*/
public function addWeight(string $date, float $weight)
{
$this->doPost('/modern/proxy/weight-service/user-weight', [
'value' => $weight,
'unitKey' => 'kg',
'date' => $date,
]);
}
/**
* @return null|string
*/
protected function getTokenFromCookie(): ?string
{
$cookies = $this->getCookies();
if (!empty($cookies['SESSIONID']) && $cookies['SESSIONID']['expiration'] > time()) {
return $cookies['SESSIONID']['value'];
}
return null;
}
protected function doGet(string $relativeUrl, array $data = [])
{
return $this->doRequest('GET', $relativeUrl, $data);
}
protected function doPost(string $relativeUrl, array $data)
{
return $this->doRequest('POST', $relativeUrl, $data);
}
protected function doRequest(string $method, string $relativeUrl, array $data)
{
$url = (strpos($relativeUrl, 'https') === false ? 'https://connect.garmin.com' : '') . $relativeUrl;
$cookieFileName = $this->getCookieFileName();
$dataQuery = http_build_query($data);
$ch = curl_init();
switch ($method) {
case 'GET':
if (!empty($dataQuery)) {
$url .= '?' . $dataQuery;
}
break;
case 'POST':
if (strpos($relativeUrl, '/modern/') !== false) {
$dataQuery = json_encode($data);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'content-type: application/json',
'accept: application/json, text/javascript, */*; q=0.01',
'accept-encoding: gzip, deflate, br',
'accept-language: fr-FR,fr;q=0.9,en-GB;q=0.8,en;q=0.7,fr-CH;q=0.6,de-CH;q=0.5,de;q=0.4,it-CH;q=0.3,it;q=0.2,en-US;q=0.1',
'cache-control: no-cache',
'pragma: no-cache',
'authority: connect.garmin.com',
'adrum: isAjax:true',
'dnt: 1',
'nk: NT',
'origin: https://connect.garmin.com',
'referer: https://connect.garmin.com/modern/weight',
'x-app-ver: 4.8.0.12',
'x-lang: en-US',
'x-requested-with: XMLHttpRequest',
]);
}
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataQuery);
break;
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFileName);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFileName);
//curl_setopt($ch, CURLOPT_VERBOSE, true);
//$verbose = fopen('php://temp', 'w+');
//curl_setopt($ch, CURLOPT_STDERR, $verbose);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
if ($result === FALSE) {
printf("cUrl error (#%d): %s\n", curl_errno($ch), curl_error($ch));
//rewind($verbose);
//$verboseLog = stream_get_contents($verbose);
//echo "Verbose information:\n", $verboseLog, "\n";
}
curl_close($ch);
if (strpos($relativeUrl, '/modern/') !== false && substr($result, 0, 15) !== '<!DOCTYPE html>') {
if ($info['http_code'] === 200) {
return json_decode($result, true);
}
}
return $result;
}
}