Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set our own Key when adding an object to Firebase via REST API

I have use following to add object to Firebase database.

$url = "https://myfb.firebaseio.com/api/types/ty/packs.json";
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);                               
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $userData);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
        $jsonResponse = curl_exec($ch);
        if(curl_errno($ch))
        {
            echo 'Curl error: ' . curl_error($ch);
        }
 curl_close($ch);

The $userData will be kept under uniquely generated node in our database as follows. enter image description here

I want to give my custom key to that node. How I do this?

like image 500
isuru Avatar asked Oct 30 '22 15:10

isuru


1 Answers

If you are using Firebase REST API to insert data in Firebase using your custom Id and without any child(auto-generated) id, use 'Put' Instead of 'Post'.

like image 181
Rida Noor Avatar answered Nov 17 '22 05:11

Rida Noor