-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgetdata.php
More file actions
57 lines (46 loc) · 1.61 KB
/
getdata.php
File metadata and controls
57 lines (46 loc) · 1.61 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
<?php
// Get data from Fitbit Website through its API
include_once("config.php");
// VALIDATE THE VARIABLES
// Step 1 - Validation of Date Availability
if (isset($_GET['date'])) {
$date = $_GET['date'];
}
else {
exit("No date information is provided. Please provide date in YYYY-MM-DD format.");
}
// Step 2 - Validation of Date Format
if (!validateDate($date . ' 00:00:00')) {
exit("Date is not in a valid format. Please use YYYY-MM-DD Format. Sorry.");
}
// Step 3 - Validation of Access Token
if (!isset($_SESSION['fb_access_token']) || trim($_SESSION['fb_access_token']) == "") {
exit("No access token available. Please <a href='./index.php'>try again</a>.");
} else {
$access_token = trim($_SESSION['fb_access_token']);
}
// Step 4 - Validation of Client ID
if (!isset($_SESSION['fb_client_id']) || trim($_SESSION['fb_client_id']) == "") {
exit("No access token available. Please <a href='./index.php'>try again</a>.");
} else {
$fb_client_id = trim($_SESSION['fb_client_id']);
}
// GET DATA FROM FITBIT
// Step 1 - Prepare URL
$url = "$config_api_url/$date/1d/1sec/time/00:00/23:59.json";
$headers = array("Authorization: Bearer $access_token");
// Step 2 - Get from Fitbit
$data = curl($url,$headers);
// Step 3 - Sanitize
$date = safe($date);
$user = safe($fb_client_id);
$data = safe($data);
// Step 4 - Insert into database
$q = "insert into `data_raw` set fbuser = '$user', fbdate='$date', fbjsondata='$data'
ON DUPLICATE KEY update fbjsondata='$data'";
if (mysqli_query($config_conn, $q)) {
header("Location: data.php?date=$date&rand=".rand());
} else {
exit("There was an error synchronising with Fitbit");
}
?>