1
|
|
|
<?php |
2
|
|
|
namespace Firebase; |
3
|
|
|
|
4
|
|
|
require_once __DIR__ . '/firebaseInterface.php'; |
5
|
|
|
|
6
|
|
|
use Exception; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Firebase PHP Client Library |
10
|
|
|
* |
11
|
|
|
* @author Tamas Kalman <[email protected]> |
12
|
|
|
* @url https://github.com/ktamas77/firebase-php/ |
13
|
|
|
* @link https://www.firebase.com/docs/rest-api.html |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Firebase PHP Class |
18
|
|
|
* |
19
|
|
|
* @author Tamas Kalman <[email protected]> |
20
|
|
|
* @link https://www.firebase.com/docs/rest-api.html |
21
|
|
|
*/ |
22
|
|
|
class FirebaseLib implements FirebaseInterface |
23
|
|
|
{ |
24
|
|
|
private $baseURI; |
25
|
|
|
private $timeout; |
26
|
|
|
private $token; |
27
|
|
|
private $curlHandler; |
28
|
|
|
private $sslConnection; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Constructor |
32
|
|
|
* |
33
|
|
|
* @param string $baseURI |
34
|
|
|
* @param string $token |
35
|
|
|
*/ |
36
|
|
|
function __construct($baseURI = '', $token = '') |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
if ($baseURI === '') { |
39
|
|
|
trigger_error('You must provide a baseURI variable.', E_USER_ERROR); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (!extension_loaded('curl')) { |
43
|
|
|
trigger_error('Extension CURL is not loaded.', E_USER_ERROR); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->setBaseURI($baseURI); |
47
|
|
|
$this->setTimeOut(10); |
48
|
|
|
$this->setToken($token); |
49
|
|
|
$this->initCurlHandler(); |
50
|
|
|
$this->setSSLConnection(true); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Initializing the CURL handler |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
public function initCurlHandler() |
59
|
|
|
{ |
60
|
|
|
$this->curlHandler = curl_init(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Closing the CURL handler |
65
|
|
|
* |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
|
|
public function closeCurlHandler() |
69
|
|
|
{ |
70
|
|
|
curl_close($this->curlHandler); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Enabling/Disabling SSL Connection |
75
|
|
|
* |
76
|
|
|
* @param bool $enableSSLConnection |
77
|
|
|
*/ |
78
|
|
|
public function setSSLConnection($enableSSLConnection) { |
79
|
|
|
$this->sslConnection = $enableSSLConnection; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Returns status of SSL Connection |
84
|
|
|
* |
85
|
|
|
* @return boolean |
86
|
|
|
*/ |
87
|
|
|
public function getSSLConnection() |
88
|
|
|
{ |
89
|
|
|
return $this->sslConnection; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Sets Token |
94
|
|
|
* |
95
|
|
|
* @param string $token Token |
96
|
|
|
* |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
|
|
public function setToken($token) |
100
|
|
|
{ |
101
|
|
|
$this->token = $token; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Sets Base URI, ex: http://yourcompany.firebase.com/youruser |
106
|
|
|
* |
107
|
|
|
* @param string $baseURI Base URI |
108
|
|
|
* |
109
|
|
|
* @return void |
110
|
|
|
*/ |
111
|
|
|
public function setBaseURI($baseURI) |
112
|
|
|
{ |
113
|
|
|
$baseURI .= (substr($baseURI, -1) === '/' ? '' : '/'); |
114
|
|
|
$this->baseURI = $baseURI; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Returns with the normalized JSON absolute path |
119
|
|
|
* |
120
|
|
|
* @param string $path Path |
121
|
|
|
* @param array $options Options |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
private function getJsonPath($path, $options = array()) |
125
|
|
|
{ |
126
|
|
|
$url = $this->baseURI; |
127
|
|
|
if ($this->token !== '') { |
128
|
|
|
$options['auth'] = $this->token; |
129
|
|
|
} |
130
|
|
|
$path = ltrim($path, '/'); |
131
|
|
|
$query = http_build_query($options); |
132
|
|
|
return "$url$path.json?$query"; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Sets REST call timeout in seconds |
137
|
|
|
* |
138
|
|
|
* @param integer $seconds Seconds to timeout |
139
|
|
|
* |
140
|
|
|
* @return void |
141
|
|
|
*/ |
142
|
|
|
public function setTimeOut($seconds) |
143
|
|
|
{ |
144
|
|
|
$this->timeout = $seconds; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Writing data into Firebase with a PUT request |
149
|
|
|
* HTTP 200: Ok |
150
|
|
|
* |
151
|
|
|
* @param string $path Path |
152
|
|
|
* @param mixed $data Data |
153
|
|
|
* @param array $options Options |
154
|
|
|
* |
155
|
|
|
* @return array Response |
156
|
|
|
*/ |
157
|
|
|
public function set($path, $data, array $options = []) |
158
|
|
|
{ |
159
|
|
|
return $this->writeData($path, $data, 'PUT', $options); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Pushing data into Firebase with a POST request |
164
|
|
|
* HTTP 200: Ok |
165
|
|
|
* |
166
|
|
|
* @param string $path Path |
167
|
|
|
* @param mixed $data Data |
168
|
|
|
* @param array $options Options |
169
|
|
|
* |
170
|
|
|
* @return array Response |
171
|
|
|
*/ |
172
|
|
|
public function push($path, $data, array $options = []) |
173
|
|
|
{ |
174
|
|
|
return $this->writeData($path, $data, 'POST', $options); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Updating data into Firebase with a PATH request |
179
|
|
|
* HTTP 200: Ok |
180
|
|
|
* |
181
|
|
|
* @param string $path Path |
182
|
|
|
* @param mixed $data Data |
183
|
|
|
* @param array $options Options |
184
|
|
|
* |
185
|
|
|
* @return array Response |
186
|
|
|
*/ |
187
|
|
|
public function update($path, $data, array $options = []) |
188
|
|
|
{ |
189
|
|
|
return $this->writeData($path, $data, 'PATCH', $options); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Reading data from Firebase |
194
|
|
|
* HTTP 200: Ok |
195
|
|
|
* |
196
|
|
|
* @param string $path Path |
197
|
|
|
* @param array $options Options |
198
|
|
|
* |
199
|
|
|
* @return array Response |
200
|
|
|
*/ |
201
|
|
|
public function get($path, array $options = []) |
202
|
|
|
{ |
203
|
|
|
try { |
204
|
|
|
$ch = $this->getCurlHandler($path, 'GET', $options); |
205
|
|
|
$return = curl_exec($ch); |
206
|
|
|
} catch (Exception $e) { |
207
|
|
|
$return = null; |
208
|
|
|
} |
209
|
|
|
return $return; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Deletes data from Firebase |
214
|
|
|
* HTTP 204: Ok |
215
|
|
|
* |
216
|
|
|
* @param string $path Path |
217
|
|
|
* @param array $options Options |
218
|
|
|
* |
219
|
|
|
* @return array Response |
220
|
|
|
*/ |
221
|
|
|
public function delete($path, array $options = []) |
222
|
|
|
{ |
223
|
|
|
try { |
224
|
|
|
$ch = $this->getCurlHandler($path, 'DELETE', $options); |
225
|
|
|
$return = curl_exec($ch); |
226
|
|
|
} catch (Exception $e) { |
227
|
|
|
$return = null; |
228
|
|
|
} |
229
|
|
|
return $return; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Returns with Initialized CURL Handler |
234
|
|
|
* |
235
|
|
|
* @param string $path Path |
236
|
|
|
* @param string $mode Mode |
237
|
|
|
* @param array $options Options |
238
|
|
|
* |
239
|
|
|
* @return resource Curl Handler |
240
|
|
|
*/ |
241
|
|
|
private function getCurlHandler($path, $mode, $options = array()) |
242
|
|
|
{ |
243
|
|
|
$url = $this->getJsonPath($path, $options); |
244
|
|
|
$ch = $this->curlHandler; |
245
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
246
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); |
247
|
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout); |
248
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->getSSLConnection()); |
249
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
250
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $mode); |
251
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
252
|
|
|
return $ch; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
private function writeData($path, $data, $method = 'PUT', $options = array()) |
256
|
|
|
{ |
257
|
|
|
$jsonData = json_encode($data); |
258
|
|
|
$header = array( |
259
|
|
|
'Content-Type: application/json', |
260
|
|
|
'Content-Length: ' . strlen($jsonData) |
261
|
|
|
); |
262
|
|
|
try { |
263
|
|
|
$ch = $this->getCurlHandler($path, $method, $options); |
264
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); |
265
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); |
266
|
|
|
$return = curl_exec($ch); |
267
|
|
|
} catch (Exception $e) { |
268
|
|
|
$return = null; |
269
|
|
|
} |
270
|
|
|
return $return; |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.