Skip to content

Instantly share code, notes, and snippets.

@daif
Created November 20, 2017 05:45
Show Gist options
  • Save daif/48cbf135d85525fd90c286820cb3cb22 to your computer and use it in GitHub Desktop.
Save daif/48cbf135d85525fd90c286820cb3cb22 to your computer and use it in GitHub Desktop.
<?php
// set your database path
$firebase = 'https://myrealtimedb.firebaseio.com/';
// Update or Save to fireBase
$data = FireBaseJson('PUT','MyContentPath', 'MyContentData');
print_r($data);
// Get from fireBase
$data = FireBaseJson('GET', 'MyContentPath');
print_r($data);
// Delete from firebase
//$data = FireBaseJson('DELETE', 'MyContentPath');
//print_r($data);
function FireBaseJson($action, $path, $data='') {
global $firebase;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $firebase.$path.'.json');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($action));
if(strtoupper($action) == 'POST' || strtoupper($action) == 'PUT' || strtoupper($action) == 'PATCH') {
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment