-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFroxlorAPI.class.php
203 lines (179 loc) · 4.04 KB
/
FroxlorAPI.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
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/** Prevent direct access to this file */
if (defined('APPLICATION') === false) {
die('Direct access not permitted!');
}
class FroxlorAPI
{
/**
* URL to api.php of your froxlor installation
*
* @var string
*/
private $host = "";
/**
* your api-key
*
* @var string
*/
private $api_key = "";
/**
* your api-secret
*
* @var string
*/
private $api_secret = "";
/**
* last cURL error message
*
* @var string
*/
private $last_error = "";
/**
* last response header received
*
* @var array
*/
private $last_header = array();
/**
* last response data received
*
* @var array
*/
private $last_body = array();
/**
* create FroxlorAPI object
*
* @param string $host
* URL to api.php of your froxlor installation
* @param string $api_key
* your api-key
* @param string $api_secret
* your api-secret
*
* @return FroxlorAPI
*/
public function __construct(string $host, string $api_key, string $api_secret)
{
$this->host = $host;
$this->api_key = $api_key;
$this->api_secret = $api_secret;
}
/**
* send request to froxlor api
*
* @param string $command
* @param array $params
*
* @return FroxlorAPI
*/
public function request(string $command, array $params = array()): FroxlorAPI
{
// build request array
$request = [
'header' => [
'apikey' => $this->api_key,
'secret' => $this->api_secret
],
'body' => [
'command' => $command
]
];
// add parameter to request-body if any
if (!empty($params)) {
$request['body']['params'] = $params;
}
// reset last data
$this->last_header = array();
$this->last_body = array();
// send actual request
$response = $this->requestCurl(json_encode($request));
// decode response
$resp = json_decode($response[1], true);
// set body to data-part of response
$this->last_body = $resp['data'];
// set header of response
$this->last_header = [
'status' => $resp['status'],
'status_message' => $resp['status_message']
];
// check for error in api response
if (isset($this->last_header['status']) && $this->last_header['status'] >= 400) {
// set last-error message
$this->last_error .= "[" . $this->last_header['status'] . "] " . $this->last_header['status_message'];
}
return $this;
}
/**
* returns last response header
*
* @return array status|status_message
*/
public function getLastHeader(): array
{
return $this->last_header;
}
/**
* returns last response data
*
* @return array
*/
public function getLastResponse(): array
{
if (!empty($this->getLastError())) {
// nothing is returned when the last call
// was not successful
return [];
}
return $this->last_body;
}
/**
* return last known error message
*
* @return string
*/
public function getLastError(): string
{
return $this->last_error;
}
/**
* send cURL request to api
*
* @param string $data
* json array
*
* @return array header|body
*/
private function requestCurl(string $data): array
{
// reset last error message
$this->last_error = "";
$ch = curl_init($this->host);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/json'
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
$verbose = fopen('php://temp', 'w+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
if (!$data = curl_exec($ch)) {
$this->last_error = 'Curl execution error: ' . curl_error($ch) . "\n";
rewind($verbose);
$verboseLog = stream_get_contents($verbose);
$this->last_error .= "Verbose information: " . htmlspecialchars($verboseLog) . "\n";
}
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($data, 0, $header_size);
$body = substr($data, $header_size);
curl_close($ch);
return array(
$header,
$body
);
}
}