-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.class.php
executable file
·191 lines (148 loc) · 4.85 KB
/
api.class.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
<?php
/** This file (class?) accesses data from MapMyRide.com. */
class API {
private $debug = FALSE;
private $tokens = array();
private $tokenFilename = "tokens.json";
/** The address of the MapMyRide API server which we do HTTP requests to. */
//private $domain = "https://api.ua.com/";
private $domain = "https://oauth2-api.mapmyapi.com/";
/** The MapMyRide API version string which is used in the URL of the HTTP request. */
private $apiVersion = "v7.1";
/** Our developer keys which identify this unique application. */
private $keys = array(
"clientid"=>"6844bx9zczxph7atjzp8vj248bb97wsf",
"clientsecret"=>"hApBdsTJAnRG8eF8GgJdWhgXm8SZkvgF3zyxHdbaQV8"
);
function __construct() {
$this->tokens = $this->readTokens();
$this->log("Tokens:");
$this->log($this->tokens);
}
private function readTokens() {
$fstring = file_get_contents($this->tokenFilename);
return json_decode($fstring,TRUE);
}
public function getTokens() {
return $this->tokens;
}
private function getAPIUrl($endpoint) {
return $this->domain . $this->apiVersion . $endpoint;
}
public function getWorkout($workoutid) {
if (!$workoutid) { die("Workoutid is required."); }
$endpoint = "/workout/{$workoutid}/";
$fields = array(
"id"=>$workoutid,
"field_set"=>"time_series"
);
$workout = $this->GET($this->getAPIUrl($endpoint),$fields);
return $workout;
}
public function getWorkouts($fields) {
$endpoint = '/workout/';
//finally, do the GET request.
$workout = $this->GET($this->getAPIUrl($endpoint),$fields);
return $workout;
}
private function GET($url,$fields) {
$fields_string = '?';
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
$this->log("fields:");
$this->log($fields_string);
//open connection
$ch = curl_init();
// debugging!
if ($this->debug === TRUE) {
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
}
//set the url, number of vars, data
$toURL = $url . $fields_string;
// set request headers
$headers = array(
'Authorization: Bearer ' . $this->tokens["access_token"],
'Api-Key: ' . $this->keys["clientid"],
);
$this->log("Sending GET request to: {$toURL}");
curl_setopt($ch, CURLOPT_URL, $toURL);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//execute GET request
$result = curl_exec($ch);
if (curl_errno($ch)) {
$this->log("Curl error:");
die(curl_error($ch));
curl_close($ch);
} else {
switch(curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
case 200:
$this->log("200 Success!");
// if JSON content, decode!
if (curl_getinfo($ch, CURLINFO_CONTENT_TYPE) == "application/json") {
$this->log("it's JSON!");
$result = json_decode($result,TRUE);
}
break;
case 403: //permission denied?!
$this->log("Error 403: Permission Denied!");
break;
case 404: //endpoint not found!
$this->log("Error 404: Endpoint (or API URL)");
break;
case 400: //Bad request
$this->log("Error 400: Bad Request");
break;
default: //unidentified error
$this->log("Error:");
break;
}
//get header info
$this->log(curl_getinfo($ch));
$this->log($result);
}
curl_close($ch);
return $result;
}
private function log($data) {
if ($this->debug === TRUE) {
print"<br />";
var_dump($data);
}
return;
}
}
Class Convert {
private $metersPerKilometer = 1000;
private $kilometersPerMile = 1.609344;
private $mphPerMps = 2.23693629;
private $joulesPerKcal = 4184;
public function mpsToMph($val) {
// convert meters per second to miles per hour
return $val * $this->mphPerMps;
}
public function metersToMiles($val) {
// convert distance from meters to kilometers to miles
return ($val / $this->metersPerKilometer) / $this->kilometersPerMile;
}
public function joulesToKCals($val) {
// convert joules to kcal (calories burned during workout)
return $val / $this->joulesPerKcal;
}
public function secondsToTimeString($seconds) {
// convert seconds (time) to hh:mm:ss
// adapted from: http://codeaid.net/php/convert-seconds-to-hours-minutes-and-seconds-(php)
// extract hours
$hours = floor($seconds / (60 * 60));
// extract minutes
$divisor_for_minutes = $seconds % (60 * 60);
$minutes = floor($divisor_for_minutes / 60);
// extract the remaining seconds
$divisor_for_seconds = $divisor_for_minutes % 60;
$seconds = ceil($divisor_for_seconds);
return $hours . ":" . $minutes . ":" . $seconds;
}
}
?>