# How to Integrate Firebase with PHP

In this article you will learn, how to connect firebase with php backend, how to use firebase realtime database with php, how to use firebase service account to connect PHP with firebase in detail.

Just follow the few steps.

  • 1. Create New Project in Firebase
  • 2. Create Real-Time Database in Firebase
  • 3. Download Secret Json File From Firebase
  • 4. Create PHP File
  • 5. Download Firebase PHP SDK ( kreait/firebase-php ) Using Composer Dependency Manager
  • 6. Setup Firebase PHP SDK ( kreait/firebase-php ) and Connect to Firebase
  • 7. Use Firebase "Realtime Database" in PHP

Step 1. Create New Project in Firebase

After Signin you will get this page.

c. Now click on "Go to console". If you are creating an account first time on firebase you will get welcome page.

d. Click on "Create a project"

If you have pre-build project in firebase, you will get this page. Here you can click on "Add Project"

e. Enter your project name and click on "Continue"

f. Click on "Continue". You will get below page.

g. Just click on "Create Project". And your project will be created successfuly in just seconds. ( Step 1 Completed )

After Project created successfully, you will get firebase dashboard.

Step 2. Create Real-Time Database in Firebase

a. Click on "Realtime Database" under "Build" Dropdown.

b. Click on "Create Database".

c. Select your database server location and click on "Next" button.

d. Choose an option "Start in test mode" and click on "Enable" button.

Your database is now created successfully. You will get below page. ( Step 2 Completed )

Step 3. Download Secret Json File From Firebase

a. Hover on "Project Overview" in top left corner and click on "Project Settings".

b. Below Page will open after clicking on "Project Settings". On that page click on "Service accounts"

c. Scroll Down to this "Service accounts" page and click on "Generate new private key" button as shown below.

d. Click on "Generate Key" button. A .json file will download in your system. Keep this file save as this be used in php project. ( Step 3 Completed )

Step 4. Create PHP File

a. Create an empty folder in your htdoc folder of your xampp server. In my case its "FirebaseProject"

b. Open this "FirebaseProject" folder in your favourite code editor. In my case its "Visual Studio Code"

c. Creata new file with ".php" extension ( a php file ) in "FirebaseProject" folder. In my case its "index.php"

Now we have an empty "index.php" file in "FirebaseProject" folder. ( Step 4 Completed )

Step 5. Download Firebase PHP SDK (kreait/firebase-php) Using Composer Dependency Manager

a. In your project root folder run below composer command to download (kreait/firebase-php) sdk.

composer require kreait/firebase-php

After Composer command run successfully, Your project structure will look like below screeshot. ( Step 6 completed )

Step 6. Setup Firebase PHP SDK ( kreait/firebase-php ) and Connect to Firebase

a. After installing, you need to require Composer's autoloader in your index.php file. ( Where you use to add firebase realtime database )

require __DIR__.'/vendor/autoload.php';

b. Rename .json file that you have downloaded from firebase service account to secret.json and paste it in your project root folder.

Your project will have these files, If you are following this guide.

c. You need to create factory instance in order to use firebase features like realtime database.

Following code will create a new factory instance.

require __DIR__.'/vendor/autoload.php';
use Kreait\Firebase\Factory;
$factory = (new Factory)->withServiceAccount('secret.json')->withDatabaseUri('https://my-second-project-d7b39-default-rtdb.firebaseio.com');

Code Explain:

use Kreait\Firebase\Factory; This line will import Factory class from vendor folder to index.php file. Its a namespace

secret.json (file that we have downloaded from service account of firebase)

https://my-second-project-d7b39-default-rtdb.firebaseio.com (this is the filrebase realtime database path). See below screeshot

d. By using factory instance , we can create Firebase Realtime Database instance and make database opetations.

Following code will create Firebase Realtime Database instance.

$database = $factory->createDatabase();

Now we have created our database instance. We are ready to make operations on it. ( Step 6 completed )

Step 7. Use Firebase "Realtime Database" in PHP

a. Now we have database instance by using this we can perform any database operations. For example: To make read operation we have to access database reference

$reference = $database->getReference('path/to/child/location');
$snapshot = $reference->getSnapshot();
$value = $snapshot->getValue();

Code Explain:

"path/to/child/location" is the location of node that you want to access data from. For example in this case its "company".

Final code to get all company data will be :

require __DIR__.'/vendor/autoload.php';
use Kreait\Firebase\Factory;
$factory = (new Factory)->withServiceAccount('secret.json')->withDatabaseUri('https://my-second-project-d7b39-default-rtdb.firebaseio.com');
$database = $factory->createDatabase();
$reference = $database->getReference('company');
$snapshot = $reference->getSnapshot();
$value = $snapshot->getValue();


Firebase PHP PHP Firebase Realtime Database Firebase PHP connection

Thanks for reading.

Related Articles

Complete guide about firebase realtime database intergration with Laravel and making operations on it.

Complete guide about firestore database integration with Laravel and making operations on it.