Skip to content

Instantly share code, notes, and snippets.

@krhoyt
Created December 6, 2014 19:29
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save krhoyt/655a892d315167194516 to your computer and use it in GitHub Desktop.
Save krhoyt/655a892d315167194516 to your computer and use it in GitHub Desktop.
Interact with Firebase from PHP.
<?php
// Constants
$FIREBASE = "_YOUR_FIREBASE_URL_";
$NODE_DELETE = "temperature.json";
$NODE_GET = "temperature.json";
$NODE_PATCH = ".json";
$NODE_PUT = "temperature.json";
// Data for PUT
// Node replaced
$data = 32;
// Data for PATCH
// Matching nodes updated
$data = array(
"temperature" => 42
);
// JSON encoded
$json = json_encode( $data );
// Initialize cURL
$curl = curl_init();
// Create
// curl_setopt( $curl, CURLOPT_URL, $FIREBASE . $NODE_PUT );
// curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, "PUT" );
// curl_setopt( $curl, CURLOPT_POSTFIELDS, 32 );
// Read
// curl_setopt( $curl, CURLOPT_URL, $FIREBASE . $NODE_GET );
// Update
curl_setopt( $curl, CURLOPT_URL, $FIREBASE . $NODE_PATCH );
curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, "PATCH" );
curl_setopt( $curl, CURLOPT_POSTFIELDS, $json );
// Delete
// curl_setopt( $curl, CURLOPT_URL, $FIREBASE . $NODE_DELETE );
// curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, "DELETE" );
// Get return value
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
// Make request
// Close connection
$response = curl_exec( $curl );
curl_close( $curl );
// Show result
echo $response . "\n";
?>
@xaxtian
Copy link

xaxtian commented Jun 7, 2016

Great!!!
It works perfectly!!!

@jokerrider007
Copy link

Woww
so cool.

@TheBennyU
Copy link

It is exactly what I was looking for ! Thanks !

@zeeshanjan82
Copy link

Thanks a lot!

@shivam1singh
Copy link

delete operation is not working for me its throw a error "Array ( [error] => Unsupported operation )". so can you please help me with this. Thanks

@EmanRajput
Copy link

It's not working !! output shows nothing !! where i have made mistake

@EmanRajput
Copy link

image

@gomathyfollowon
Copy link

gomathyfollowon commented Feb 8, 2018

Thanks for sharing code.
i need to update particular field and rest other fields in firebase database.
i tried your code
curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, "PATCH" );
while using PATCH it make empty the other fields.
For example here i tried to update "plugin" field update. Find the attachment.
can you help me? thanks in advance.

ADD
firebaseadd

UPDATE
firebaseupdate

@DavidInnocent
Copy link

What about the Firebase data that needs authentication for access. How would you handle that?

@ziadasem
Copy link

great

@krhoyt
Copy link
Author

krhoyt commented Sep 13, 2019

Thank you all for the comments and feedback. Note that I have not worked with Firebase for more than a year, and as such the API may have changed, and this code may be out of date (probably is out of date). I honestly never expected that people would find this gist and use it - I just posted it for my own memory. I am not actively maintaining this code.

@HarshSharma1234
Copy link

I am facing problem in below mentioned code ,I am beginner please help!
How to setup??
// Constants
$FIREBASE = "YOUR_FIREBASE_URL";
$NODE_DELETE = "temperature.json";
$NODE_GET = "temperature.json";
$NODE_PATCH = ".json";
$NODE_PUT = "temperature.json";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment