-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFleetlogAPI.php
281 lines (239 loc) · 5.5 KB
/
FleetlogAPI.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
<?php
/**
* Fleetlog-API : Simple PHP wrapper for the v2 API
*
* PHP version 5.3.10
*
* @package Fleetlog-API
* @author Viktor Sincak <viktor@fleetlog.com.au>
* @license Apache-2.0
* @version 0.0.3
* @link https://github.com/fleetlog/fleetlog-php
*/
class FleetlogAPI
{
/**
* @var string
*/
private $oauth_access_token;
/**
* @var array
*/
private $postfields;
/**
* @var string
*/
private $getfield;
/**
* @var string
*/
private $baseUrl;
/**
* @var string
*/
public $url;
/**
* @var string
*/
public $requestMethod;
/**
* Create the API access object. Requires an array of settings::
* oauth access token
* These are all available by creating your own application on fleetog.com.au
* Requires the cURL library
*
* @throws \Exception When cURL isn't installed or incorrect settings parameters are provided
*
* @param array $settings
*/
public function __construct(array $settings = NULL)
{
if (!in_array('curl', get_loaded_extensions()))
{
throw new Exception('You need to install cURL, see: http://curl.haxx.se/docs/install.html');
}
if (isset($settings['oauth_access_token']))
{
$this->oauth_access_token = $settings['oauth_access_token'];
} else {
$this->oauth_access_token = NULL;
}
$this->baseUrl = 'https://api.fleetlog.com.au/v2/';
}
/**
* Set access token
*
* @param $accessToken
*/
public function setAccessToken($accessToken)
{
$this->oauth_access_token = $accessToken;
}
/**
* Set postfields array, example: array('screen_name' => 'asd')
*
* @param array $array Array of parameters to send to API
*
* @throws \Exception When you are trying to set both get and post fields
*
* @return FleetlogAPI Instance of self for method chaining
*/
public function setPostfields(array $array = NULL)
{
if (!is_null($this->getGetfield()))
{
throw new Exception('You can only choose get OR post fields.');
}
if (isset($array['status']) && substr($array['status'], 0, 1) === '@')
{
$array['status'] = sprintf("\0%s", $array['status']);
}
$this->postfields = $array;
return $this;
}
/**
* Set getfield string, example: '?screen_name=J7mbo'
*
* @param string $string Get key and value pairs as string
*
* @throws \Exception
*
* @return \FleetlogAPI Instance of self for method chaining
*/
public function setGetfield($string)
{
if (!is_null($this->getPostfields()))
{
throw new Exception('You can only choose get OR post fields.');
}
$getfields = preg_replace('/^\?/', '', explode('&', $string));
$params = array();
foreach ($getfields as $field)
{
if ($field !== '')
{
list($key, $value) = explode('=', $field);
$params[$key] = $value;
}
}
$this->getfield = '?' . http_build_query($params);
return $this;
}
/**
* Get getfield string (simple getter)
*
* @return string $this->getfields
*/
public function getGetfield()
{
return $this->getfield;
}
/**
* Get postfields array (simple getter)
*
* @return array $this->postfields
*/
public function getPostfields()
{
return $this->postfields;
}
/**
* Resets the fields to allow a new query
* with different method
*/
public function resetFields() {
$this->postfields = null;
$this->getfield = null;
$this->url = '';
return $this;
}
/**
* Perform the actual data retrieval from the API
*
* @param array $curlOptions Additional Curl options for this request
*
* @throws \Exception
*
* @return string json
*/
public function performRequest($curlOptions = array())
{
if (!is_null($this->oauth_access_token)) {
$headers = ['Authorization: Bearer '.$this->oauth_access_token];
} else {
$headers = [];
}
// For /token [POST] route
if (in_array('Content-type: application/x-www-form-urlencoded', $curlOptions))
{
array_push($headers, 'Content-type: application/x-www-form-urlencoded');
} else
{
array_push($headers, 'Content-type: application/json');
}
$getfield = $this->getGetfield();
$postfields = $this->getPostfields();
$options = array(
CURLOPT_HTTPHEADER => $headers,
CURLOPT_HEADER => false,
CURLOPT_URL => $this->baseUrl.$this->url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
) + $curlOptions;
if (!is_null($postfields))
{
$options[CURLOPT_POSTFIELDS] = http_build_query($postfields);
}
else
{
if ($getfield !== '')
{
$options[CURLOPT_URL] .= $getfield;
}
}
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
if (($error = curl_error($feed)) !== '')
{
curl_close($feed);
throw new \Exception($error);
}
curl_close($feed);
// reset fields
$this->resetFields();
return json_decode($json);
}
/**
* Helper method to perform our request
*
* @param string $url
* @param string $method
* @param string $data
* @param array $curlOptions
*
* @throws \Exception
*
* @return string The json response from the server
*/
public function request($url, $method = 'get', $data = null, $curlOptions = array())
{
$this->url = $url;
if (strtolower($method) === 'get')
{
$this->setGetfield($data);
}
if (strtolower($method) === 'post'
|| strtolower($method) === 'put'
|| strtolower($method) === 'patch')
{
$this->setPostfields($data);
$curlOptions + array('CURLOPT_CUSTOMREQUEST' => strtoupper($method));
}
if (strtolower($method) === 'delete')
{
$curlOptions + array('CURLOPT_CUSTOMREQUEST' => 'DELETE');
}
return $this->performRequest($curlOptions);
}
}