I have a situation where in, I have to create a php script which keeps listening to Firebase node and whenever any change happens in the node it reads data and performs update/delete/add operations at few other places in the same Firebase database.
Is this possible using PHP?
I came across few things while exploring for this like.. Firebase streaming apis, Somewhat similar stackoverflow thread
I have written following sample code.
$fb_url = 'https://my_firebase_url/userObjects/userCommunities.json?auth='.FIREBASE_SECRET;
//Init curl session
$ch = curl_init();
//Set URL
curl_setopt($ch, CURLOPT_URL, $fb_url);
curl_setopt($ch, CURLOPT_NOBODY, 0);
//Set header
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept: text/event-stream'));
// Set callback function for headers
//curl_setopt($ch, CURLOPT_HEADERFUNCTION, ‘read_header’);
curl_setopt($ch, CURLOPT_USERAGENT, '');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
// Set callback function for body
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'read_body');
//Execute curl
curl_exec($ch);
//close curl session
curl_close($ch);
//Callback function for body
function read_body($ch, $string)
{
//$length = strlen($string);
//echo "Received $length bytes\n";
print_r($string);exit;
return $length;
}
When I open this script in browser or run in background (using SSH), it callbacks the read_body
function but it always returns me specific chunk of data from subscribed node.
Nothing happens when I modify some data in Firebase.
How am I supposed to implement a background service to listen to Firebase node and update another Firebase nodes in PHP?
Is this even possible using PHP? or do I have to look for another alternative language? What those languages will be?
Any links which gives better explanation will be a great help.
Thanks