-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathXivelyAPI.php
488 lines (459 loc) · 13.4 KB
/
XivelyAPI.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
<?php
/**
* Xively API class
* Version 0.4 (June 2012)
* Requirements: PHP5, cURL, API v.2.0
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
class XivelyAPI
{
private $Api;
private $Xively;
private $Xively_headers;
/**
* Constructor
*/
function __construct($api)
{
$this->Api = $api;
$this->Xively = "api.xively.com/v2";
$this->Xively_headers = array("X-ApiKey: $this->Api");
}
/**
* Get list of feeds
* @param string format of output ("json", "xml", "csv")
* @param int page number
* @param int feeds per page
* @param string content type ("full", "summary")
* @param string query for full text search
* @param string tag
* @param string user name
* @param string units
* @param string status ("live", "frozen", "all")
* @param string order ("created_at", "retrieved_at", "relevance")
* @param array location (lat, lon, distance, distance_units)
* @return string
*/
public function getFeedsList($format=false, $page=false, $per_page=false, $content=false, $query=false, $tag=false, $user=false, $units=false, $status=false, $order=false, $location=false)
{
if($format && ($format == "json" || $format == "csv" || $format == "xml")) $f = ".". $format;
$url = "https://$this->Xively/feeds$f?";
if($page) $url .= "page=" . $page . "&";
if($per_page) $url .= "per_page=" . $per_page . "&";
if($content) $url .= "content=" . $content . "&";
if($query) $url .= "q=" . $query . "&";
if($tag) $url .= "tag=" . $tag . "&";
if($user) $url .= "user=" . $user . "&";
if($units) $url .= "units=" . $units . "&";
if($status) $url .= "status=" . $status . "&";
if($order) $url .= "order=" . $order . "&";
if($location)
{
$url .= "lat=" . $location['lat'] . "&" . "lon=" . $location['lon'] . "&";
if(isset($location['distance'])) $url .= "distance=" . $location['distance'] . "&";
if(isset($location['distance_units'])) $url .= "distance_units=" . $location['distance_units'];
}
return $this->_getRequest($url);
}
/**
* Get feed information
* @param string format of output ("json", "xml", "csv")
* @param int feed ID
* @param string/array datastreams
* @return string
*/
public function getFeed($format=false, $feed, $datastreams=false)
{
if($format && ($format == "json" || $format == "csv" || $format == "xml")) $feed .= ".". $format;
$url = "https://$this->Xively/feeds/$feed?";
if($datastreams)
{
if(is_array($datastreams))
{
$url .= "datastreams=" . implode(",", $datastreams);
}
else
{
$url .= "datastreams=" . $datastreams;
}
}
return $this->_getRequest($url);
}
/**
* Update feed with data
* @param int feed ID
* @param string data to update
* @return http response headers
*/
public function updateFeed($format=false, $feed, $data)
{
if($format && ($format == "json" || $format == "csv" || $format == "xml")) $feed .= ".". $format;
$url = "https://$this->Xively/feeds/$feed";
return $this->_putRequest($url, $data);
}
/**
* Delete feed from Xively
* @param int feed ID
* @return http response headers
*/
public function deleteFeed($feed)
{
$url = "https://$this->Xively/feeds/$feed";
return $this->_deleteRequest($url);
}
/**
* Get feed information
* @param int feed ID
* @return array of objects
*/
public function getDatastreamsList($feed)
{
$feed .= ".json";
$url = "https://$this->Xively/feeds/$feed";
$data = json_decode($this->_getRequest($url));
return $data->datastreams;
}
/**
* Create datastream
* @param int feed ID
* @param string datastream name
* @param int datastream ID
* @return http response headers
*/
public function createDatastream($format=false, $feed, $data)
{
$url = "https://$this->Xively/feeds/$feed/datastreams";
if($format && ($format == "json" || $format == "csv" || $format == "xml")) $url .= ".". $format;
return $this->_postRequest($url, $data);
}
/**
* Get datastream
* @param string format of output ("json", "xml", "csv")
* @param int feed ID
* @param string datastream ID
* @return string
*/
public function getDatastream($format=false, $feed, $datastream)
{
if($format && ($format == "json" || $format == "csv" || $format == "xml")) $datastream .= ".". $format;
$url = "https://$this->Xively/feeds/$feed/datastreams/$datastream";
return $this->_getRequest($url);
}
/**
* Update datastream
* @param string format of output ("json", "xml", "csv")
* @param int feed ID
* @param string datastream ID
* @param string data
* @return http response headers
*/
public function updateDatastream($format=false, $feed, $datastream, $data)
{
if($format && ($format == "json" || $format == "csv" || $format == "xml")) $datastream .= ".". $format;
$url = "https://$this->Xively/feeds/$feed/datastreams/$datastream";
return $this->_putRequest($url, $data);
}
/**
* Delete datastream
* @param int feed ID
* @param string datastream ID
* @return http response headers
*/
public function deleteDatastream($feed, $datastream)
{
$url = "https://$this->Xively/feeds/$feed/datastreams/$datastream";
return $this->_deleteRequest($url);
}
// ToDo: DataPoints
// ToDo: Triggers
// ToDo: Users: List, Create, Update, Delete
// ToDo: API keys
/**
* Get user information
* @param string format of output ("json", "xml")
* @param string user login
* @return string
*/
public function getUser($format=false, $user)
{
if($format && ($format == "json" || $format == "xml")) $user .= ".". $format;
$url = "https://$this->Xively/users/$user";
return $this->_getRequest($url);
}
/**
* Get feed history
* @param string format of output ("json", "xml", "csv")
* @param int feed ID
* @param string start point
* @param string end point
* @param string duration
* @param int page
* @param int per_page
* @param string time
* @param bool find_previous
* @param string interval_type ("discrete", false)
* @param int interval (in seconds: 0, 30, 60, 300, 900, 3600, 10800, 21600, 43200, 86400)
* @param string timezone
* @return string
*/
public function getFeedHistory($format, $feed, $start=false, $end=false, $duration=false, $page=false, $per_page=false, $time=false, $find_previous=false, $interval_type=false, $interval=false, $timezone=false)
{
if($format && ($format == "json" || $format == "csv" || $format == "xml")) $feed .= ".". $format;
$url = "https://$this->Xively/feeds/$feed?";
if($start) $url .= "start=" . $start . "&";
if($end) $url .= "end=" . $end . "&";
if($duration) $url .= "duration=" . $duration . "&";
if($page) $url .= "page=" . $page . "&";
if($per_page) $url .= "per_page=" . $per_page . "&";
if($time) $url .= "time=" . $time . "&";
if($find_previous) $url .= "find_previous=" . $find_previous . "&";
if($interval_type) $url .= "interval_type=" . $interval_type . "&";
if($interval) $url .= "interval=" . $interval;
if($timezone) $url .= "timezone=" . $timezone;
return $this->_getRequest($url);
}
/**
* Get datastream history
* @param string format of output ("json", "xml", "csv")
* @param int feed ID
* @param string datastream ID
* @param string start point
* @param string end point
* @param string duration
* @param int page
* @param int per_page
* @param string time
* @param bool find_previous
* @param string interval_type ("discrete", false)
* @param int interval (in seconds: 0, 30, 60, 300, 900, 3600, 10800, 21600, 43200, 86400)
* @param string timezone
* @return string
*/
public function getDatastreamHistory($format, $feed, $datastream, $start=false, $end=false, $duration=false, $page=false, $per_page=false, $time=false, $find_previous=false, $interval_type=false, $interval=false, $timezone=false)
{
if($format && ($format == "json" || $format == "csv" || $format == "xml")) $datastream .= ".". $format;
$url = "https://$this->Xively/feeds/$feed/datastreams/$datastream?";
if($start) $url .= "start=" . $start . "&";
if($end) $url .= "end=" . $end . "&";
if($duration) $url .= "duration=" . $duration . "&";
if($page) $url .= "page=" . $page . "&";
if($per_page) $url .= "per_page=" . $per_page . "&";
if($time) $url .= "time=" . $time . "&";
if($find_previous) $url .= "find_previous=" . $find_previous . "&";
if($interval_type) $url .= "interval_type=" . $interval_type . "&";
if($interval) $url .= "interval=" . $interval;
if($timezone) $url .= "timezone=" . $timezone;
return $this->_getRequest($url);
}
/**
* Create GET request to Xively (wrapper)
* @param string url
* @return http code response
*/
private function _getRequest($url)
{
if(function_exists('curl_init'))
{
return $this->_curl($url, true);
}
elseif(function_exists('file_get_contents') && ini_get('allow_url_fopen'))
{
return $this->_get($url);
}
else
{
return 500;
}
}
/**
* Create POST request to Xively (wrapper)
* @param string url
* @param array data
* @return http code response
*/
private function _postRequest($url, $data)
{
if(function_exists('curl_init'))
{
return $this->_curl($url, true, true, $data);
}
elseif(function_exists('file_post_contents') && ini_get('allow_url_fopen'))
{
return $this->_post($url, $data);
}
else
{
return 500;
}
}
/**
* Create PUT request to Xively (wrapper)
* @param string url
* @param string data
* @return http code response
*/
private function _putRequest($url, $data)
{
if(function_exists('curl_init'))
{
$putData = tmpfile();
fwrite($putData, $data);
fseek($putData, 0);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->Xively_headers);
curl_setopt($ch, CURLOPT_INFILE, $putData);
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($data));
curl_setopt($ch, CURLOPT_PUT, true);
curl_exec($ch);
$headers = curl_getinfo($ch);
fclose($putData);
curl_close($ch);
return $headers['http_code'];
}
elseif(function_exists('file_put_contents') && ini_get('allow_url_fopen'))
{
return $this->_put($url,$data);
}
else
{
return 500;
}
}
/**
* Create DELETE request to Xively
* @param string url
* @return http code response
*/
private function _deleteRequest($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->Xively_headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_exec($ch);
$headers = curl_getinfo($ch);
curl_close($ch);
return $headers['http_code'];
}
/**
* GET requests to Xively
* @param string url
* @return response
*/
private function _get($url)
{
// Create a stream
$opts['http']['method'] = "GET";
$opts['http']['header'] = "X-ApiKey: ".$this->Api."\r\n";
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
return file_get_contents($url, false, $context);
}
/**
* POST requests to Xively
* @param string url
* @param array data
* @return response
*/
private function _post($url, $data)
{
$postfields = http_build_query($data);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postfields,
)
);
$context = stream_context_create($opts);
return file_get_contents($url, false, $context);
}
/**
* PUT requests to Xively
* @param string url
* @param string data
* @return response
*/
private function _put($url,$data)
{
// Create a stream
$opts['http']['method'] = "PUT";
$opts['http']['header'] = "X-ApiKey: ".$this->Api."\r\n";
$opts['http']['header'] .= "Content-Length: " . strlen($data) . "\r\n";
$opts['http']['content'] = $data;
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
return file_get_contents($url, false, $context);
}
/**
* cURL main function
* @param string url
* @param bool authentication
* @return response
*/
private function _curl($url, $auth=false, $post=false, $post_data=false)
{
if(function_exists('curl_init'))
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if($auth) curl_setopt($ch, CURLOPT_HTTPHEADER, $this->Xively_headers);
if($post)
{
curl_setopt($ch, CURLOPT_POST, 1);
if($post_data) curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
else
{
return false;
}
}
/**
* Print debug status of error
* @param int status code
*/
public function _debugStatus($status_code)
{
switch ($status_code)
{
case 200:
$msg = "Xively feed successfully updated";
break;
case 401:
$msg = "Xively API key was incorrect";
break;
case 403:
$msg = "Access forbidden!";
break;
case 404:
$msg = "Feed ID or some other parameter does not exist";
break;
case 422:
$msg = "Unprocessable Entity, semantic errors (CSV instead of XML?)";
break;
case 418:
$msg = "Error in feed ID, data type or some other data";
break;
case 500:
$msg = "cURL library not installed or some other internal error occured";
break;
default:
$msg = "Status code not recognised: ".$status_code;
break;
}
echo $msg;
}
}
?>