-
Notifications
You must be signed in to change notification settings - Fork 0
/
microBeesSDK.php
115 lines (111 loc) · 4.13 KB
/
microBeesSDK.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
<?php
class microBees {
public function __construct(array $arguments = array()) {
if (!empty($arguments)) {
foreach ($arguments as $property => $argument) {
$this->{$property} = $argument;
}
}
}
public function __call($method, $arguments) {
$arguments = array_merge(array("microBees" => $this), $arguments); // Note: method argument 0 will always referred to the main class ($this).
if (isset($this->{$method}) && is_callable($this->{$method})) {
return call_user_func_array($this->{$method}, $arguments);
} else {
throw new Exception("Fatal error: Call to undefined method microBees::{$method}()");
}
}
private function json_validate($string){
$result = json_decode($string);
switch (json_last_error()) {
case JSON_ERROR_NONE:
$error = ''; // JSON is valid // No error has occurred
break;
case JSON_ERROR_DEPTH:
$error = 'The maximum stack depth has been exceeded.';
break;
case JSON_ERROR_STATE_MISMATCH:
$error = 'Invalid or malformed JSON.';
break;
case JSON_ERROR_CTRL_CHAR:
$error = 'Control character error, possibly incorrectly encoded.';
break;
case JSON_ERROR_SYNTAX:
$error = 'Syntax error, malformed JSON.';
break;
// PHP >= 5.3.3
case JSON_ERROR_UTF8:
$error = 'Malformed UTF-8 characters, possibly incorrectly encoded.';
break;
// PHP >= 5.5.0
case JSON_ERROR_RECURSION:
$error = 'One or more recursive references in the value to be encoded.';
break;
// PHP >= 5.5.0
case JSON_ERROR_INF_OR_NAN:
$error = 'One or more NAN or INF values in the value to be encoded.';
break;
case JSON_ERROR_UNSUPPORTED_TYPE:
$error = 'A value of a type that cannot be encoded was given.';
break;
default:
$error = 'Unknown JSON error occured.';
break;
}
if ($error !== '') {
if($this->DEBUG)
echo "<br/><hr/>Fatal error: ".$error;
else throw new Exception("Fatal error: ".$error);
}
return $result;
}
private $token = "";
private $DEBUG = false;
public function getAccessToken($username,$password,$clientID,$clientSecret){
$endpoint="https://dev.microbees.com/oauth/token?grant_type=password&username=".$username."&password=".$password."&client_id=".$clientID."&client_secret=".$clientSecret."&redirect_uri=";
$curl = curl_init($endpoint);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-type: application/json; ',
'charset: UTF-8'
)
);
$json_response = curl_exec($curl);
curl_close($curl);
$response = $this->json_validate($json_response, true);
if(!isset($response->{"error"}))
$token = $json_response->access_token;
if($this->DEBUG)
print_r($json_response);
}
public function doRequest($url,$params){
if(preg_match('/[^,:{}\\[\\]0-9.\\-+Eaeflnr-u \\n\\r\\t]/',preg_replace('/"(\\.|[^"\\\\])*"/', '', $params))){
$content = $this->json_validate($params);
}
$content = $params;
$url="https://dev.microbees.com/".$url;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-type: application/json; ',
'charset: UTF-8',
'Content-Length: ' . strlen($params),
'Authorization: Bearer '.$this->token
)
);
$json_response = curl_exec($curl);
curl_close($curl);
$response = json_decode($json_response, true);
if($this->DEBUG){
echo "<br/><hr/>";
print_r($json_response);
}
return $json_response;
}
}
?>