forked from pavelrisenberg/fitbitphp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fitbitphp.php
2346 lines (2099 loc) · 81.9 KB
/
fitbitphp.php
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* FitbitPHP v.0.70. Basic Fitbit API wrapper for PHP using OAuth
*
* Note: Library is in beta and provided as-is. We hope to add features as API grows, however
* feel free to fork, extend and send pull requests to us.
*
* - https://github.com/heyitspavel/fitbitphp
*
*
* Date: 2011/12/09
* Requires OAuth 1.0.0, SimpleXML
* @version 0.70 ($Id$)
*/
class FitBitPHP
{
/**
* API Constants
*
*/
private $authHost = 'www.fitbit.com';
private $apiHost = 'api.fitbit.com';
private $baseApiUrl;
private $authUrl;
private $requestTokenUrl;
private $accessTokenUrl;
/**
* Class Variables
*
*/
protected $oauth;
protected $oauth_Token, $oauth_Secret;
protected $userId = '-';
protected $metric = 0;
protected $userAgent = 'FitbitPHP 0.70';
protected $debug;
protected $clientDebug;
/**
* @param string $consumer_key Application consumer key for Fitbit API
* @param string $consumer_secret Application secret
* @param int $debug Debug mode (0/1) enables OAuth internal debug
* @param string $userAgent User-agent to use in API calls
*/
public function __construct($consumer_key, $consumer_secret, $debug = 1, $userAgent = null)
{
$this->initUrls();
$this->consumer_key = $consumer_key;
$this->consumer_secret = $consumer_secret;
$this->oauth = new OAuth($consumer_key, $consumer_secret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);
$this->debug = $debug;
if (isset($userAgent))
$this->userAgent = $userAgent;
if ($debug)
$this->oauth->enableDebug();
}
/**
* @param string $consumer_key Application consumer key for Fitbit API
* @param string $consumer_secret Application secret
*/
public function reinit($consumer_key, $consumer_secret)
{
$this->consumer_key = $consumer_key;
$this->consumer_secret = $consumer_secret;
$this->oauth = new OAuth($consumer_key, $consumer_secret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);
if ($debug)
$this->oauth->enableDebug();
}
/**
* @param string $apiHost API host, i.e. api.fitbit.com (do you know any others?)
* @param string $authHost Auth host, i.e. www.fitbit.com
*/
public function setEndpointBase($apiHost, $authHost, $https = true, $httpsApi = false)
{
$this->apiHost = $apiHost;
$this->authHost = $authHost;
$this->initUrls($https, $httpsApi);
}
private function initUrls($https = true, $httpsApi = false)
{
if ($httpsApi)
$this->baseApiUrl = 'https://' . $this->apiHost . '/1/';
else
$this->baseApiUrl = 'http://' . $this->apiHost . '/1/';
if ($https) {
$this->authUrl = 'https://' . $this->authHost . '/oauth/authorize';
$this->requestTokenUrl = 'https://' . $this->apiHost . '/oauth/request_token';
$this->accessTokenUrl = 'https://' . $this->apiHost . '/oauth/access_token';
} else {
$this->authUrl = 'http://' . $this->authHost . '/oauth/authorize';
$this->requestTokenUrl = 'http://' . $this->apiHost . '/oauth/request_token';
$this->accessTokenUrl = 'http://' . $this->apiHost . '/oauth/access_token';
}
}
/**
* @return OAuth debugInfo object for previous call. Debug should be enabled in __construct
*/
public function oauthDebug()
{
return $this->oauth->debugInfo;
}
/**
* @return OAuth debugInfo object for previous client_customCall. Debug should be enabled in __construct
*/
public function client_oauthDebug()
{
return $this->clientDebug;
}
/**
* Returns Fitbit session status for frontend (i.e. 'Sign in with Fitbit' implementations)
*
* @return int (0 - no session, 1 - just after successful authorization, 2 - session exist)
*/
public static function sessionStatus()
{
$session = session_id();
if (empty($session)) {
session_start();
}
if (empty($_SESSION['fitbit_Session']))
$_SESSION['fitbit_Session'] = 0;
return (int)$_SESSION['fitbit_Session'];
}
/**
* Initialize session. Inits OAuth session, handles redirects to Fitbit login/authorization if needed
*
* @param $callbackUrl Callback for 'Sign in with Fitbit'
* @param $cookie Use persistent cookie for authorization, or session cookie only
* @return int (1 - just after successful authorization, 2 - if session already exist)
*/
public function initSession($callbackUrl, $cookie = true)
{
$session = session_id();
if (empty($session)) {
session_start();
}
if (empty($_SESSION['fitbit_Session']))
$_SESSION['fitbit_Session'] = 0;
if (!isset($_GET['oauth_token']) && $_SESSION['fitbit_Session'] == 1)
$_SESSION['fitbit_Session'] = 0;
if ($_SESSION['fitbit_Session'] == 0) {
$request_token_info = $this->oauth->getRequestToken($this->requestTokenUrl, $callbackUrl);
$_SESSION['fitbit_Secret'] = $request_token_info['oauth_token_secret'];
$_SESSION['fitbit_Session'] = 1;
header('Location: ' . $this->authUrl . '?oauth_token=' . $request_token_info['oauth_token']);
exit;
} else if ($_SESSION['fitbit_Session'] == 1) {
$this->oauth->setToken($_GET['oauth_token'], $_SESSION['fitbit_Secret']);
$access_token_info = $this->oauth->getAccessToken($this->accessTokenUrl);
$_SESSION['fitbit_Session'] = 2;
$_SESSION['fitbit_Token'] = $access_token_info['oauth_token'];
$_SESSION['fitbit_Secret'] = $access_token_info['oauth_token_secret'];
$this->setOAuthDetails($_SESSION['fitbit_Token'], $_SESSION['fitbit_Secret']);
return 1;
} else if ($_SESSION['fitbit_Session'] == 2) {
$this->setOAuthDetails($_SESSION['fitbit_Token'], $_SESSION['fitbit_Secret']);
return 2;
}
}
/**
* Reset session
*
* @return void
*/
public function resetSession()
{
$_SESSION['fitbit_Session'] = 0;
}
/**
* Sets OAuth token/secret. Use if library used in internal calls without session handling
*
* @param $token
* @param $secret
* @return void
*/
public function setOAuthDetails($token, $secret)
{
$this->oauth_Token = $token;
$this->oauth_Secret = $secret;
$this->oauth->setToken($this->oauth_Token, $this->oauth_Secret);
}
/**
* Get OAuth token
*
* @return string
*/
public function getOAuthToken()
{
return $this->oauth_Token;
}
/**
* Get OAuth secret
*
* @return string
*/
public function getOAuthSecret()
{
return $this->oauth_Secret;
}
/**
* Set Fitbit userId for future API calls
*
* @param $userId 'XXXXX'
* @return void
*/
public function setUser($userId)
{
$this->userId = $userId;
}
/**
* Set Unit System for all future calls (see http://wiki.fitbit.com/display/API/API-Unit-System)
* 0 (Metric), 1 (en_US), 2 (en_GB)
*
* @param int $metric
* @return void
*/
public function setMetric($metric)
{
$this->metric = $metric;
}
/**
* API wrappers
*
*/
/**
* Get user profile
*
* @throws FitBitException
* @param string $userId UserId of public profile, if none using set with setUser or '-' by default
* @return SimpleXMLElement
*/
public function getProfile()
{
$headers = $this->getHeaders();
try {
$this->oauth->fetch($this->baseApiUrl . "user/" . $this->userId . "/profile.xml", null, OAUTH_HTTP_METHOD_GET, $headers);
} catch (Exception $E) {
}
$response = $this->oauth->getLastResponse();
$responseInfo = $this->oauth->getLastResponseInfo();
if (!strcmp($responseInfo['http_code'], '200')) {
$xml = simplexml_load_string($response);
if ($xml)
return $xml;
else
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
} else {
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
}
}
/**
* Update user profile
*
* @throws FitBitException
* @param string $gender 'FEMALE', 'MALE' or 'NA'
* @param DateTime $birthday Date of birth
* @param string $height Height in cm/inches (as set with setMetric)
* @param string $nickname Nickname
* @param string $fullName Full name
* @param string $timezone Timezone in the format 'America/Los_Angeles'
* @return SimpleXMLElement
*/
public function updateProfile($gender = null, $birthday = null, $height = null, $nickname = null, $fullName = null, $timezone = null)
{
$headers = $this->getHeaders();
$parameters = array();
if (isset($gender))
$parameters['gender'] = $gender;
if (isset($birthday))
$parameters['birthday'] = $birthday->format('Y-m-d');
if (isset($height))
$parameters['height'] = $height;
if (isset($nickname))
$parameters['nickname'] = $nickname;
if (isset($fullName))
$parameters['fullName'] = $fullName;
if (isset($timezone))
$parameters['timezone'] = $timezone;
try {
$this->oauth->fetch($this->baseApiUrl . "user/-/profile.xml",
$parameters, OAUTH_HTTP_METHOD_POST, $headers);
} catch (Exception $E) {
}
$response = $this->oauth->getLastResponse();
$responseInfo = $this->oauth->getLastResponseInfo();
if (!strcmp($responseInfo['http_code'], '201')) {
$xml = simplexml_load_string($response);
if ($xml)
return $xml;
else
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
} else {
$xml = simplexml_load_string($response);
if (!$xml)
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
else
throw new FitBitException($responseInfo['http_code'], $xml->errors->apiError->message, 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
}
}
/**
* Get user activities for specific date
*
* @throws FitBitException
* @param DateTime $date
* @param String $dateStr
* @return SimpleXMLElement
*/
public function getActivities($date, $dateStr = null)
{
$headers = $this->getHeaders();
if (!isset($dateStr)) {
$dateStr = $date->format('Y-m-d');
}
try {
$this->oauth->fetch($this->baseApiUrl . "user/" . $this->userId . "/activities/date/" . $dateStr . ".xml",
null, OAUTH_HTTP_METHOD_GET, $headers);
} catch (Exception $E) {
}
$response = $this->oauth->getLastResponse();
$responseInfo = $this->oauth->getLastResponseInfo();
if (!strcmp($responseInfo['http_code'], '200')) {
$xml = simplexml_load_string($response);
if ($xml)
return $xml;
else
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
} else {
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
}
}
/**
* Get user recent activities
*
* @throws FitBitException
* @return SimpleXMLElement
*/
public function getRecentActivities()
{
$headers = $this->getHeaders();
try {
$this->oauth->fetch($this->baseApiUrl . "user/-/activities/recent.xml", null,
OAUTH_HTTP_METHOD_GET, $headers);
} catch (Exception $E) {
}
$response = $this->oauth->getLastResponse();
$responseInfo = $this->oauth->getLastResponseInfo();
if (!strcmp($responseInfo['http_code'], '200')) {
$xml = simplexml_load_string($response);
if ($xml)
return $xml;
else
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
} else {
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
}
}
/**
* Get user frequent activities
*
* @throws FitBitException
* @return SimpleXMLElement
*/
public function getFrequentActivities()
{
$headers = $this->getHeaders();
try {
$this->oauth->fetch($this->baseApiUrl . "user/-/activities/frequent.xml", null,
OAUTH_HTTP_METHOD_GET, $headers);
} catch (Exception $E) {
}
$response = $this->oauth->getLastResponse();
$responseInfo = $this->oauth->getLastResponseInfo();
if (!strcmp($responseInfo['http_code'], '200')) {
$xml = simplexml_load_string($response);
if ($xml)
return $xml;
else
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
} else {
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
}
}
/**
* Get user favorite activities
*
* @throws FitBitException
* @return SimpleXMLElement
*/
public function getFavoriteActivities()
{
$headers = $this->getHeaders();
try {
$this->oauth->fetch($this->baseApiUrl . "user/-/activities/favorite.xml", null,
OAUTH_HTTP_METHOD_GET, $headers);
} catch (Exception $E) {
}
$response = $this->oauth->getLastResponse();
$responseInfo = $this->oauth->getLastResponseInfo();
if (!strcmp($responseInfo['http_code'], '200')) {
$xml = simplexml_load_string($response);
if ($xml)
return $xml;
else
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
} else {
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
}
}
/**
* Log user activity
*
* @throws FitBitException
* @param DateTime $date Activity date and time (set proper timezone, which could be fetched via getProfile)
* @param string $activityId Activity Id (or Intensity Level Id) from activities database,
* see http://wiki.fitbit.com/display/API/API-Log-Activity
* @param string $duration Duration millis
* @param string $calories Manual calories to override Fitbit estimate
* @param string $distance Distance in km/miles (as set with setMetric)
* @param string $distanceUnit Distance unit string (see http://wiki.fitbit.com/display/API/API-Distance-Unit)
* @return SimpleXMLElement
*/
public function logActivity($date, $activityId, $duration, $calories = null, $distance = null, $distanceUnit = null, $activityName = null)
{
$distanceUnits = array('Centimeter', 'Foot', 'Inch', 'Kilometer', 'Meter', 'Mile', 'Millimeter', 'Steps', 'Yards');
$headers = $this->getHeaders();
$parameters = array();
$parameters['date'] = $date->format('Y-m-d');
$parameters['startTime'] = $date->format('H:i');
if (isset($activityName)) {
$parameters['activityName'] = $activityName;
$parameters['manualCalories'] = $calories;
} else {
$parameters['activityId'] = $activityId;
if (isset($calories))
$parameters['manualCalories'] = $calories;
}
$parameters['durationMillis'] = $duration;
if (isset($distance))
$parameters['distance'] = $distance;
if (isset($distanceUnit) && in_array($distanceUnit, $distanceUnits))
$parameters['distanceUnit'] = $distanceUnit;
try {
$this->oauth->fetch($this->baseApiUrl . "user/-/activities.xml", $parameters,
OAUTH_HTTP_METHOD_POST, $headers);
} catch (Exception $E) {
}
$response = $this->oauth->getLastResponse();
$responseInfo = $this->oauth->getLastResponseInfo();
if (!strcmp($responseInfo['http_code'], '201')) {
$xml = simplexml_load_string($response);
if ($xml)
return $xml;
else
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
} else {
$xml = simplexml_load_string($response);
if (!$xml)
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
else
throw new FitBitException($responseInfo['http_code'], $xml->errors->apiError->message, 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
}
}
/**
* Delete user activity
*
* @throws FitBitException
* @param string $id Activity log id
* @return bool
*/
public function deleteActivity($id)
{
$headers = $this->getHeaders();
try {
$this->oauth->fetch($this->baseApiUrl . "user/-/activities/" . $id . ".xml", null,
OAUTH_HTTP_METHOD_DELETE, $headers);
} catch (Exception $E) {
}
$responseInfo = $this->oauth->getLastResponseInfo();
if (!strcmp($responseInfo['http_code'], '204')) {
return true;
} else {
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
}
}
/**
* Add user favorite activity
*
* @throws FitBitException
* @param string $id Activity log id
* @return bool
*/
public function addFavoriteActivity($id)
{
$headers = $this->getHeaders();
try {
$this->oauth->fetch($this->baseApiUrl . "user/-/activities/log/favorite/" . $id . ".xml",
null, OAUTH_HTTP_METHOD_POST, $headers);
} catch (Exception $E) {
}
$responseInfo = $this->oauth->getLastResponseInfo();
if (!strcmp($responseInfo['http_code'], '201')) {
return true;
} else {
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
}
}
/**
* Delete user favorite activity
*
* @throws FitBitException
* @param string $id Activity log id
* @return bool
*/
public function deleteFavoriteActivity($id)
{
$headers = $this->getHeaders();
try {
$this->oauth->fetch($this->baseApiUrl . "user/-/activities/log/favorite/" . $id . ".xml",
null, OAUTH_HTTP_METHOD_DELETE, $headers);
} catch (Exception $E) {
}
$responseInfo = $this->oauth->getLastResponseInfo();
if (!strcmp($responseInfo['http_code'], '204')) {
return true;
} else {
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
}
}
/**
* Get full description of specific activity
*
* @throws FitBitException
* @param string $id Activity log Id
* @return SimpleXMLElement
*/
public function getActivity($id)
{
$headers = $this->getHeaders();
try {
$this->oauth->fetch($this->baseApiUrl . "activities/" . $id . ".xml", null,
OAUTH_HTTP_METHOD_GET, $headers);
} catch (Exception $E) {
}
$response = $this->oauth->getLastResponse();
$responseInfo = $this->oauth->getLastResponseInfo();
if (!strcmp($responseInfo['http_code'], '200')) {
$xml = simplexml_load_string($response);
if ($xml)
return $xml;
else
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
} else {
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
}
}
/**
* Get a tree of all valid Fitbit public activities as well as private custom activities the user createds
*
* @throws FitBitException
* @return SimpleXMLElement
*/
public function browseActivities()
{
$headers = $this->getHeaders();
try {
$this->oauth->fetch($this->baseApiUrl . "activities.xml", null,
OAUTH_HTTP_METHOD_GET, $headers);
} catch (Exception $E) {
}
$response = $this->oauth->getLastResponse();
$responseInfo = $this->oauth->getLastResponseInfo();
if (!strcmp($responseInfo['http_code'], '200')) {
$xml = simplexml_load_string($response);
if ($xml)
return $xml;
else
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
} else {
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
}
}
/**
* Get user foods for specific date
*
* @throws FitBitException
* @param DateTime $date
* @param String $dateStr
* @return SimpleXMLElement
*/
public function getFoods($date, $dateStr = null)
{
$headers = $this->getHeaders();
if (!isset($dateStr)) {
$dateStr = $date->format('Y-m-d');
}
try {
$this->oauth->fetch($this->baseApiUrl . "user/" . $this->userId . "/foods/log/date/" . $dateStr . ".xml",
null, OAUTH_HTTP_METHOD_GET, $headers);
} catch (Exception $E) {
}
$response = $this->oauth->getLastResponse();
$responseInfo = $this->oauth->getLastResponseInfo();
if (!strcmp($responseInfo['http_code'], '200')) {
$xml = simplexml_load_string($response);
if ($xml)
return $xml;
else
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
} else {
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
}
}
/**
* Get user recent foods
*
* @throws FitBitException
* @return SimpleXMLElement
*/
public function getRecentFoods()
{
$headers = $this->getHeaders();
try {
$this->oauth->fetch($this->baseApiUrl . "user/-/foods/log/recent.xml", null,
OAUTH_HTTP_METHOD_GET, $headers);
} catch (Exception $E) {
}
$response = $this->oauth->getLastResponse();
$responseInfo = $this->oauth->getLastResponseInfo();
if (!strcmp($responseInfo['http_code'], '200')) {
$xml = simplexml_load_string($response);
if ($xml)
return $xml;
else
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
} else {
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
}
}
/**
* Get user frequent foods
*
* @throws FitBitException
* @return SimpleXMLElement
*/
public function getFrequentFoods()
{
$headers = $this->getHeaders();
try {
$this->oauth->fetch($this->baseApiUrl . "user/-/foods/log/frequent.xml", null,
OAUTH_HTTP_METHOD_GET, $headers);
} catch (Exception $E) {
}
$response = $this->oauth->getLastResponse();
$responseInfo = $this->oauth->getLastResponseInfo();
if (!strcmp($responseInfo['http_code'], '200')) {
$xml = simplexml_load_string($response);
if ($xml)
return $xml;
else
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
} else {
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
}
}
/**
* Get user favorite foods
*
* @throws FitBitException
* @return SimpleXMLElement
*/
public function getFavoriteFoods()
{
$headers = $this->getHeaders();
try {
$this->oauth->fetch($this->baseApiUrl . "user/-/foods/log/favorite.xml", null,
OAUTH_HTTP_METHOD_GET, $headers);
} catch (Exception $E) {
}
$response = $this->oauth->getLastResponse();
$responseInfo = $this->oauth->getLastResponseInfo();
if (!strcmp($responseInfo['http_code'], '200')) {
$xml = simplexml_load_string($response);
if ($xml)
return $xml;
else
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
} else {
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
}
}
/**
* Log user food
*
* @throws FitBitException
* @param DateTime $date Food log date
* @param string $foodId Food Id from foods database (see searchFoods)
* @param string $mealTypeId Meal Type Id from foods database (see searchFoods)
* @param string $unitId Unit Id, should be allowed for this food (see getFoodUnits and searchFoods)
* @param string $amount Amount in specified units
* @return SimpleXMLElement
*/
public function logFood($date, $foodId, $mealTypeId, $unitId, $amount, $foodName = null, $calories = null, $brandName = null, $nutrition = null)
{
$headers = $this->getHeaders();
$parameters = array();
$parameters['date'] = $date->format('Y-m-d');
if (isset($foodName)) {
$parameters['foodName'] = $foodName;
$parameters['calories'] = $calories;
if (isset($brandName))
$parameters['brandName'] = $brandName;
if (isset($nutrition)) {
foreach ($nutrition as $i => $value) {
$parameters[$i] = $nutrition[$i];
}
}
} else {
$parameters['foodId'] = $foodId;
}
$parameters['mealTypeId'] = $mealTypeId;
$parameters['unitId'] = $unitId;
$parameters['amount'] = $amount;
try {
$this->oauth->fetch($this->baseApiUrl . "user/-/foods/log.xml", $parameters,
OAUTH_HTTP_METHOD_POST, $headers);
} catch (Exception $E) {
}
$response = $this->oauth->getLastResponse();
$responseInfo = $this->oauth->getLastResponseInfo();
if (!strcmp($responseInfo['http_code'], '201')) {
$xml = simplexml_load_string($response);
if ($xml)
return $xml;
else
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
} else {
$xml = simplexml_load_string($response);
if (!$xml)
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
else
throw new FitBitException($responseInfo['http_code'], $xml->errors->apiError->message, 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
}
}
/**
* Delete user food
*
* @throws FitBitException
* @param string $id Food log id
* @return bool
*/
public function deleteFood($id)
{
$headers = $this->getHeaders();
try {
$this->oauth->fetch($this->baseApiUrl . "user/-/foods/log/" . $id . ".xml", null,
OAUTH_HTTP_METHOD_DELETE, $headers);
} catch (Exception $E) {
}
$responseInfo = $this->oauth->getLastResponseInfo();
if (!strcmp($responseInfo['http_code'], '204')) {
return true;
} else {
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
}
}
/**
* Add user favorite food
*
* @throws FitBitException
* @param string $id Food log id
* @return bool
*/
public function addFavoriteFood($id)
{
$headers = $this->getHeaders();
try {
$this->oauth->fetch($this->baseApiUrl . "user/-/foods/log/favorite/" . $id . ".xml", null,
OAUTH_HTTP_METHOD_POST, $headers);
} catch (Exception $E) {
}
$responseInfo = $this->oauth->getLastResponseInfo();
if (!strcmp($responseInfo['http_code'], '201')) {
return true;
} else {
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
}
}
/**
* Delete user favorite food
*
* @throws FitBitException
* @param string $id Food log id
* @return bool
*/
public function deleteFavoriteFood($id)
{
$headers = $this->getHeaders();
try {
$this->oauth->fetch($this->baseApiUrl . "user/-/foods/log/favorite/" . $id . ".xml",
null, OAUTH_HTTP_METHOD_DELETE, $headers);
} catch (Exception $E) {
}
$responseInfo = $this->oauth->getLastResponseInfo();
if (!strcmp($responseInfo['http_code'], '204')) {
return true;
} else {
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
}
}
/**
* Get user meal sets
*
* @throws FitBitException
* @return SimpleXMLElement
*/
public function getMeals()
{
$headers = $this->getHeaders();
try {
$this->oauth->fetch($this->baseApiUrl . "user/-/meals.xml",
null, OAUTH_HTTP_METHOD_GET, $headers);
} catch (Exception $E) {
}
$response = $this->oauth->getLastResponse();
$responseInfo = $this->oauth->getLastResponseInfo();
if (!strcmp($responseInfo['http_code'], '200')) {
$xml = simplexml_load_string($response);
if ($xml)
return $xml;
else
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
} else {
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
}
}
/**
* Get food units library
*
* @throws FitBitException
* @return SimpleXMLElement
*/
public function getFoodUnits()
{
$headers = $this->getHeaders();
try {
$this->oauth->fetch($this->baseApiUrl . "foods/units.xml", null, OAUTH_HTTP_METHOD_GET, $headers);
} catch (Exception $E) {
}
$response = $this->oauth->getLastResponse();
$responseInfo = $this->oauth->getLastResponseInfo();
if (!strcmp($responseInfo['http_code'], '200')) {
$xml = simplexml_load_string($response);
if ($xml)
return $xml;
else
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
} else {
throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
}
}
/**
* Search for foods in foods database
*
* @throws FitBitException
* @param string $query Search query
* @return SimpleXMLElement
*/
public function searchFoods($query)
{
$headers = $this->getHeaders();
try {
$this->oauth->fetch($this->baseApiUrl . "foods/search.xml?query=" . rawurlencode($query), null, OAUTH_HTTP_METHOD_GET, $headers);
} catch (Exception $E) {
}
$response = $this->oauth->getLastResponse();
$responseInfo = $this->oauth->getLastResponseInfo();
if (!strcmp($responseInfo['http_code'], '200')) {
$xml = simplexml_load_string($response);
if ($xml)
return $xml;
else