-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtonicjs.php
129 lines (107 loc) · 3.32 KB
/
tonicjs.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
<?php
class TonicjsException extends Exception {
protected $result;
public function getResult() {
return $this->result;
}
public function __toString() {
$str = $this->getType() . ': ';
if ($this->code != 0) {
$str .= $this->code . ': ';
}
return $str . $this->message;
}
}
class Tonicjs {
const VERSION = '0.5.0';
protected $apiKey;
protected $apiSecret;
protected $accessToken = null;
public static $CURL_OPTS = array(
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 60,
CURLOPT_USERAGENT => "tonicjs-php-{VERSION}",
);
public function __construct($config) {
$this->setApiKey($config['apiKey']);
$this->setApiSecret($config['secret']);
}
public function setApiKey($apiKey) {
$this->apiKey = $apiKey;
return $this;
}
public function getApiKey() {
return $this->apiKey;
}
public function setApiSecret($apiSecret) {
$this->apiSecret = $apiSecret;
return $this;
}
public function getApiSecret() {
return $this->apiSecret;
}
public function makeRequest($url, $params, $ch = null) {
$params = array_merge($params, array(
'apiKey' => $this->getApiKey(),
'apiSecret' => $this->getApiSecret()
));
$curlOptions = array();
$queryString = utf8_encode(http_build_query($params, '', '&'));
$curlOptions += array(
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $queryString,
CURLOPT_URL => $url,
CURLOPT_PORT => 9292,
CURLOPT_USERAGENT => "TONICAPI",
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true
);
$response = $this->doCurlCall($curlOptions);
$response = json_decode($response['response']);
if ($response->status == "success") {
return $response;
} else {
return $response;
}
}
public function setTagValue($tonicTagID, $value) {
$tmp = $this->makeRequest("http://192.168.56.101:9292/api/field/set", array(
'id' => $tonicTagID, 'value' => $value));
return $tmp->content;
}
public function deleteTag($tonicTagID) {
$tmp = $this->makeRequest("http://192.168.56.101:9292/api/field/delete", array('id' => $tonicTagID));
return $tmp->content;
}
public function getTag($tonicTagID) {
$tmp = $this->makeRequest("http://192.168.56.101:9292/api/field/get", array('id' => $tonicTagID));
return $tmp;
}
public function getTagValue($tonicTagID) {
$tmp = $this->getTag($tonicTagID);
return $tmp->content;
}
public function getTagJSON($tonicTagID) {
$tmp = $this->getTag($tonicTagID);
return json_decode($tmp->content);
}
public function getTags(array $tagIds) {
$tmp = $this->makeRequest("http://192.168.56.101:9292/api/fields/get", array('ids' => $tagIds));
return $tmp;
}
protected function doCurlCall(array $curlOptions) {
$curl = curl_init();
curl_setopt_array($curl, $curlOptions);
$response = curl_exec($curl);
$headers = curl_getinfo($curl);
$errorNumber = curl_errno($curl);
$errorMessage = curl_error($curl);
curl_close($curl);
return compact('response', 'headers', 'errorNumber', 'errorMessage');
}
}
$tonicjs = new Tonicjs(array(
"apiKey" => "a7b5d991-fe9e-4457-855e-c66f5c8bf0ae",
"secret" => "ad524e0c26abfda218188b4cf1b67ffba3360045"
));