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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
namespace Firebase;
require_once __DIR__ . '/firebaseInterface.php';
use \Exception;
/**
* Firebase PHP Client Library
*
* @author Tamas Kalman <ktamas77@gmail.com>
* @url https://github.com/ktamas77/firebase-php/
* @link https://www.firebase.com/docs/rest-api.html
*/
/**
* Firebase PHP Class
*
* @author Tamas Kalman <ktamas77@gmail.com>
* @link https://www.firebase.com/docs/rest-api.html
*/
class FirebaseLib implements FirebaseInterface
{
private $_baseURI;
private $_timeout;
private $_token;
private $_curlHandler;
/**
* Constructor
*
* @param string $baseURI
* @param string $token
*/
function __construct($baseURI = '', $token = '')
{
if ($baseURI == '') {
trigger_error('You must provide a baseURI variable.', E_USER_ERROR);
}
if (!extension_loaded('curl')) {
trigger_error('Extension CURL is not loaded.', E_USER_ERROR);
}
$this->setBaseURI($baseURI);
$this->setTimeOut(10);
$this->setToken($token);
$this->initCurlHandler();
}
/**
* Initializing the CURL handler
*
* @return void
*/
public function initCurlHandler()
{
$this->_curlHandler = curl_init();
}
/**
* Closing the CURL handler
*
* @return void
*/
public function closeCurlHandler()
{
curl_close($this->_curlHandler);
}
/**
* Sets Token
*
* @param string $token Token
*
* @return void
*/
public function setToken($token)
{
$this->_token = $token;
}
/**
* Sets Base URI, ex: http://yourcompany.firebase.com/youruser
*
* @param string $baseURI Base URI
*
* @return void
*/
public function setBaseURI($baseURI)
{
$baseURI .= (substr($baseURI, -1) == '/' ? '' : '/');
$this->_baseURI = $baseURI;
}
/**
* Returns with the normalized JSON absolute path
*
* @param string $path Path
* @param array $options Options
* @return string
*/
private function _getJsonPath($path, $options = array())
{
$url = $this->_baseURI;
if ($this->_token !== '') {
$options['auth'] = $this->_token;
}
$path = ltrim($path, '/');
return $url . $path . '.json?' . http_build_query($options);
}
/**
* Sets REST call timeout in seconds
*
* @param integer $seconds Seconds to timeout
*
* @return void
*/
public function setTimeOut($seconds)
{
$this->_timeout = $seconds;
}
/**
* Writing data into Firebase with a PUT request
* HTTP 200: Ok
*
* @param string $path Path
* @param mixed $data Data
* @param array $options Options
*
* @return array Response
*/
public function set($path, $data, $options = array())
{
return $this->_writeData($path, $data, 'PUT', $options);
}
/**
* Pushing data into Firebase with a POST request
* HTTP 200: Ok
*
* @param string $path Path
* @param mixed $data Data
* @param array $options Options
*
* @return array Response
*/
public function push($path, $data, $options = array())
{
return $this->_writeData($path, $data, 'POST', $options);
}
/**
* Updating data into Firebase with a PATH request
* HTTP 200: Ok
*
* @param string $path Path
* @param mixed $data Data
* @param array $options Options
*
* @return array Response
*/
public function update($path, $data, $options = array())
{
return $this->_writeData($path, $data, 'PATCH', $options);
}
/**
* Reading data from Firebase
* HTTP 200: Ok
*
* @param string $path Path
* @param array $options Options
*
* @return array Response
*/
public function get($path, $options = array())
{
try {
$ch = $this->_getCurlHandler($path, 'GET', $options);
$return = curl_exec($ch);
} catch (Exception $e) {
$return = null;
}
return $return;
}
/**
* Deletes data from Firebase
* HTTP 204: Ok
*
* @param string $path Path
* @param array $options Options
*
* @return array Response
*/
public function delete($path, $options = array())
{
try {
$ch = $this->_getCurlHandler($path, 'DELETE', $options);
$return = curl_exec($ch);
} catch (Exception $e) {
$return = null;
}
return $return;
}
/**
* Returns with Initialized CURL Handler
*
* @param string $path Path
* @param string $mode Mode
* @param array $options Options
*
* @return resource Curl Handler
*/
private function _getCurlHandler($path, $mode, $options = array())
{
$url = $this->_getJsonPath($path, $options);
$ch = $this->_curlHandler;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $this->_timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->_timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $mode);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
return $ch;
}
private function _writeData($path, $data, $method = 'PUT', $options = array())
{
$jsonData = json_encode($data);
$header = array(
'Content-Type: application/json',
'Content-Length: ' . strlen($jsonData)
);
try {
$ch = $this->_getCurlHandler($path, $method, $options);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
$return = curl_exec($ch);
} catch (Exception $e) {
$return = null;
}
return $return;
}
}