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
|
|
|
public 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
|
|
|
{ |
80
|
|
|
$this->sslConnection = $enableSSLConnection; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns status of SSL Connection |
85
|
|
|
* |
86
|
|
|
* @return boolean |
87
|
|
|
*/ |
88
|
|
|
public function getSSLConnection() |
89
|
|
|
{ |
90
|
|
|
return $this->sslConnection; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Sets Token |
95
|
|
|
* |
96
|
|
|
* @param string $token Token |
97
|
|
|
* |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
|
|
public function setToken($token) |
101
|
|
|
{ |
102
|
|
|
$this->token = $token; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Sets Base URI, ex: http://yourcompany.firebase.com/youruser |
107
|
|
|
* |
108
|
|
|
* @param string $baseURI Base URI |
109
|
|
|
* |
110
|
|
|
* @return void |
111
|
|
|
*/ |
112
|
|
|
public function setBaseURI($baseURI) |
113
|
|
|
{ |
114
|
|
|
$baseURI .= (substr($baseURI, -1) === '/' ? '' : '/'); |
115
|
|
|
$this->baseURI = $baseURI; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Returns with the normalized JSON absolute path |
120
|
|
|
* |
121
|
|
|
* @param string $path Path |
122
|
|
|
* @param array $options Options |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
private function getJsonPath($path, $options = array()) |
126
|
|
|
{ |
127
|
|
|
$url = $this->baseURI; |
128
|
|
|
if ($this->token !== '') { |
129
|
|
|
$options['auth'] = $this->token; |
130
|
|
|
} |
131
|
|
|
$path = ltrim($path, '/'); |
132
|
|
|
$query = http_build_query($options); |
133
|
|
|
return "$url$path.json?$query"; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Gets info for the last request made |
138
|
|
|
* |
139
|
|
|
* @return array details from curl_getinfo |
140
|
|
|
*/ |
141
|
|
|
public function getResponseInfo() |
142
|
|
|
{ |
143
|
|
|
return curl_getinfo($this->_curlHandler); |
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Sets REST call timeout in seconds |
148
|
|
|
* |
149
|
|
|
* @param integer $seconds Seconds to timeout |
150
|
|
|
* |
151
|
|
|
* @return void |
152
|
|
|
*/ |
153
|
|
|
public function setTimeOut($seconds) |
154
|
|
|
{ |
155
|
|
|
$this->timeout = $seconds; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Writing data into Firebase with a PUT request |
160
|
|
|
* HTTP 200: Ok |
161
|
|
|
* |
162
|
|
|
* @param string $path Path |
163
|
|
|
* @param mixed $data Data |
164
|
|
|
* @param array $options Options |
165
|
|
|
* |
166
|
|
|
* @return array Response |
167
|
|
|
*/ |
168
|
|
|
public function set($path, $data, array $options = []) |
169
|
|
|
{ |
170
|
|
|
return $this->writeData($path, $data, 'PUT', $options); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Pushing data into Firebase with a POST request |
175
|
|
|
* HTTP 200: Ok |
176
|
|
|
* |
177
|
|
|
* @param string $path Path |
178
|
|
|
* @param mixed $data Data |
179
|
|
|
* @param array $options Options |
180
|
|
|
* |
181
|
|
|
* @return array Response |
182
|
|
|
*/ |
183
|
|
|
public function push($path, $data, array $options = []) |
184
|
|
|
{ |
185
|
|
|
return $this->writeData($path, $data, 'POST', $options); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Updating data into Firebase with a PATH request |
190
|
|
|
* HTTP 200: Ok |
191
|
|
|
* |
192
|
|
|
* @param string $path Path |
193
|
|
|
* @param mixed $data Data |
194
|
|
|
* @param array $options Options |
195
|
|
|
* |
196
|
|
|
* @return array Response |
197
|
|
|
*/ |
198
|
|
|
public function update($path, $data, array $options = []) |
199
|
|
|
{ |
200
|
|
|
return $this->writeData($path, $data, 'PATCH', $options); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Reading data from Firebase |
205
|
|
|
* HTTP 200: Ok |
206
|
|
|
* |
207
|
|
|
* @param string $path Path |
208
|
|
|
* @param array $options Options |
209
|
|
|
* |
210
|
|
|
* @return array Response |
211
|
|
|
*/ |
212
|
|
|
public function get($path, array $options = []) |
213
|
|
|
{ |
214
|
|
|
try { |
215
|
|
|
$ch = $this->getCurlHandler($path, 'GET', $options); |
216
|
|
|
$return = curl_exec($ch); |
217
|
|
|
} catch (Exception $e) { |
218
|
|
|
$return = null; |
219
|
|
|
} |
220
|
|
|
return $return; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Deletes data from Firebase |
225
|
|
|
* HTTP 204: Ok |
226
|
|
|
* |
227
|
|
|
* @param string $path Path |
228
|
|
|
* @param array $options Options |
229
|
|
|
* |
230
|
|
|
* @return array Response |
231
|
|
|
*/ |
232
|
|
|
public function delete($path, array $options = []) |
233
|
|
|
{ |
234
|
|
|
try { |
235
|
|
|
$ch = $this->getCurlHandler($path, 'DELETE', $options); |
236
|
|
|
$return = curl_exec($ch); |
237
|
|
|
} catch (Exception $e) { |
238
|
|
|
$return = null; |
239
|
|
|
} |
240
|
|
|
return $return; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Returns with Initialized CURL Handler |
245
|
|
|
* |
246
|
|
|
* @param string $path Path |
247
|
|
|
* @param string $mode Mode |
248
|
|
|
* @param array $options Options |
249
|
|
|
* |
250
|
|
|
* @return resource Curl Handler |
251
|
|
|
*/ |
252
|
|
|
private function getCurlHandler($path, $mode, $options = array()) |
253
|
|
|
{ |
254
|
|
|
$url = $this->getJsonPath($path, $options); |
255
|
|
|
$ch = $this->curlHandler; |
256
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
257
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); |
258
|
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout); |
259
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->getSSLConnection()); |
260
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
261
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $mode); |
262
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
263
|
|
|
return $ch; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
private function writeData($path, $data, $method = 'PUT', $options = array()) |
267
|
|
|
{ |
268
|
|
|
$jsonData = json_encode($data); |
269
|
|
|
$header = array( |
270
|
|
|
'Content-Type: application/json', |
271
|
|
|
'Content-Length: ' . strlen($jsonData) |
272
|
|
|
); |
273
|
|
|
try { |
274
|
|
|
$ch = $this->getCurlHandler($path, $method, $options); |
275
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); |
276
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); |
277
|
|
|
$return = curl_exec($ch); |
278
|
|
|
} catch (Exception $e) { |
279
|
|
|
$return = null; |
280
|
|
|
} |
281
|
|
|
return $return; |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.