1
|
|
|
<?php |
|
|
|
|
2
|
|
|
namespace Firebase; |
3
|
|
|
|
4
|
|
|
require_once __DIR__ . '/firebaseInterface.php'; |
5
|
|
|
|
6
|
|
|
use \Exception; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Firebase PHP Client Library |
11
|
|
|
* |
12
|
|
|
* @author Tamas Kalman <[email protected]> |
13
|
|
|
* @url https://github.com/ktamas77/firebase-php/ |
14
|
|
|
* @link https://www.firebase.com/docs/rest-api.html |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Firebase PHP Class |
19
|
|
|
* |
20
|
|
|
* @author Tamas Kalman <[email protected]> |
21
|
|
|
* @link https://www.firebase.com/docs/rest-api.html |
22
|
|
|
*/ |
23
|
|
|
class FirebaseLib implements FirebaseInterface |
24
|
|
|
{ |
25
|
|
|
private $_baseURI; |
26
|
|
|
private $_timeout; |
27
|
|
|
private $_token; |
28
|
|
|
private $_curlHandler; |
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
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Initializing the CURL handler |
54
|
|
|
* |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
|
|
public function initCurlHandler() |
58
|
|
|
{ |
59
|
|
|
$this->_curlHandler = curl_init(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Closing the CURL handler |
64
|
|
|
* |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
public function closeCurlHandler() |
68
|
|
|
{ |
69
|
|
|
curl_close($this->_curlHandler); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Sets Token |
74
|
|
|
* |
75
|
|
|
* @param string $token Token |
76
|
|
|
* |
77
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
|
|
public function setToken($token) |
80
|
|
|
{ |
81
|
|
|
$this->_token = $token; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Sets Base URI, ex: http://yourcompany.firebase.com/youruser |
86
|
|
|
* |
87
|
|
|
* @param string $baseURI Base URI |
88
|
|
|
* |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
|
|
public function setBaseURI($baseURI) |
92
|
|
|
{ |
93
|
|
|
$baseURI .= (substr($baseURI, -1) == '/' ? '' : '/'); |
94
|
|
|
$this->_baseURI = $baseURI; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns with the normalized JSON absolute path |
99
|
|
|
* |
100
|
|
|
* @param string $path Path |
101
|
|
|
* @param array $options Options |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
private function _getJsonPath($path, $options = array()) |
105
|
|
|
{ |
106
|
|
|
$url = $this->_baseURI; |
107
|
|
|
if ($this->_token !== '') { |
108
|
|
|
$options['auth'] = $this->_token; |
109
|
|
|
} |
110
|
|
|
$path = ltrim($path, '/'); |
111
|
|
|
return $url . $path . '.json?' . http_build_query($options); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Sets REST call timeout in seconds |
116
|
|
|
* |
117
|
|
|
* @param integer $seconds Seconds to timeout |
118
|
|
|
* |
119
|
|
|
* @return void |
120
|
|
|
*/ |
121
|
|
|
public function setTimeOut($seconds) |
122
|
|
|
{ |
123
|
|
|
$this->_timeout = $seconds; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Writing data into Firebase with a PUT request |
128
|
|
|
* HTTP 200: Ok |
129
|
|
|
* |
130
|
|
|
* @param string $path Path |
131
|
|
|
* @param mixed $data Data |
132
|
|
|
* @param array $options Options |
133
|
|
|
* |
134
|
|
|
* @return array Response |
135
|
|
|
*/ |
136
|
|
|
public function set($path, $data, $options = array()) |
137
|
|
|
{ |
138
|
|
|
return $this->_writeData($path, $data, 'PUT', $options); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Pushing data into Firebase with a POST request |
143
|
|
|
* HTTP 200: Ok |
144
|
|
|
* |
145
|
|
|
* @param string $path Path |
146
|
|
|
* @param mixed $data Data |
147
|
|
|
* @param array $options Options |
148
|
|
|
* |
149
|
|
|
* @return array Response |
150
|
|
|
*/ |
151
|
|
|
public function push($path, $data, $options = array()) |
152
|
|
|
{ |
153
|
|
|
return $this->_writeData($path, $data, 'POST', $options); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Updating data into Firebase with a PATH request |
158
|
|
|
* HTTP 200: Ok |
159
|
|
|
* |
160
|
|
|
* @param string $path Path |
161
|
|
|
* @param mixed $data Data |
162
|
|
|
* @param array $options Options |
163
|
|
|
* |
164
|
|
|
* @return array Response |
165
|
|
|
*/ |
166
|
|
|
public function update($path, $data, $options = array()) |
167
|
|
|
{ |
168
|
|
|
return $this->_writeData($path, $data, 'PATCH', $options); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Reading data from Firebase |
173
|
|
|
* HTTP 200: Ok |
174
|
|
|
* |
175
|
|
|
* @param string $path Path |
176
|
|
|
* @param array $options Options |
177
|
|
|
* |
178
|
|
|
* @return array Response |
179
|
|
|
*/ |
180
|
|
|
public function get($path, $options = array()) |
181
|
|
|
{ |
182
|
|
|
try { |
183
|
|
|
$ch = $this->_getCurlHandler($path, 'GET', $options); |
184
|
|
|
$return = curl_exec($ch); |
185
|
|
|
} catch (Exception $e) { |
186
|
|
|
$return = null; |
187
|
|
|
} |
188
|
|
|
return $return; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Deletes data from Firebase |
193
|
|
|
* HTTP 204: Ok |
194
|
|
|
* |
195
|
|
|
* @param string $path Path |
196
|
|
|
* @param array $options Options |
197
|
|
|
* |
198
|
|
|
* @return array Response |
199
|
|
|
*/ |
200
|
|
|
public function delete($path, $options = array()) |
201
|
|
|
{ |
202
|
|
|
try { |
203
|
|
|
$ch = $this->_getCurlHandler($path, 'DELETE', $options); |
204
|
|
|
$return = curl_exec($ch); |
205
|
|
|
} catch (Exception $e) { |
206
|
|
|
$return = null; |
207
|
|
|
} |
208
|
|
|
return $return; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Returns with Initialized CURL Handler |
213
|
|
|
* |
214
|
|
|
* @param string $path Path |
215
|
|
|
* @param string $mode Mode |
216
|
|
|
* @param array $options Options |
217
|
|
|
* |
218
|
|
|
* @return resource Curl Handler |
219
|
|
|
*/ |
220
|
|
|
private function _getCurlHandler($path, $mode, $options = array()) |
221
|
|
|
{ |
222
|
|
|
$url = $this->_getJsonPath($path, $options); |
223
|
|
|
$ch = $this->_curlHandler; |
224
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
225
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, $this->_timeout); |
226
|
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->_timeout); |
227
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
228
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
229
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $mode); |
230
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
231
|
|
|
return $ch; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
private function _writeData($path, $data, $method = 'PUT', $options = array()) |
235
|
|
|
{ |
236
|
|
|
$jsonData = json_encode($data); |
237
|
|
|
$header = array( |
238
|
|
|
'Content-Type: application/json', |
239
|
|
|
'Content-Length: ' . strlen($jsonData) |
240
|
|
|
); |
241
|
|
|
try { |
242
|
|
|
$ch = $this->_getCurlHandler($path, $method, $options); |
243
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); |
244
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); |
245
|
|
|
$return = curl_exec($ch); |
246
|
|
|
} catch (Exception $e) { |
247
|
|
|
$return = null; |
248
|
|
|
} |
249
|
|
|
return $return; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
} |
253
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.