Integrate Firebase Admin SDK with PHP server
1. Go to PHP terminal. (Minimum PHP 7.4 is required, Also, mbstring PHP extension is required).
If composer is not installed install it.
Now, in command prompt, type:
composer require kreait/firebase-php
Firebase admin sdk will be installed.
A vender folder will be generated in your root. Admin SDK components will be there inside this folder.
Now we can access the firebase sdk through this source.
2. Go to Firebase console. Create account with firebase
3. Add your project.
Go to Firebase Realtime database from the left pane of the firebase console.
Add Child (say classX). Add some data (record, say Name, Roll, Age etc) to the child classX.
In this window we will get our Realtime Database https address. It is in the form ‘https://my-proj.firebaseio.com’. Copy the address, we will need this when we will reference to our database from the Admin SDK PHP.
Now in the Realtime database page go to RULES.
Here set read and write rules for reading and writing data from the web clients.
4. Now go to Settings from the left pane.
Select Service Account
In this page click on “Generate New Private Key”. Clicking this, will ask to download the private key in the form of json file.
Download and store it in secure place.
We need this file in our PHP server.
5.Iniatialize your service account in php with the following code:
require '../../vendor/autoload.php';
use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;
6. Now get reference to your Firebase realtime database in the following way-
In the above code snippet ‘myproj-xxxx-firebase-adminsdk-ws868-123444555.json’ is the json file containing the private key of your firebase service account. We stored this file in the FrebaseCred folder inside our document root.
This $firebase is the reference to your Firebase components.
Now get the reference to your realtime database with,
$database = $firebase->createDatabase();
7. To get reference of a child ( say classX, in this case), we have to put,
$reference = $database->getReference(“classX”);