# 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:
Step 1. Create New Project in Firebase
- a. Visit https://firebase.google.com site
- b. Click on "Signin" and signin with your gmail account.
- 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 "Firestore 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 laravel 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 SDKs ( kreait/firebase-php ) and ( google/cloud-firestore ) Using Composer Dependency Manager
-
a. In your project root folder run this composer command to download (kreait/firebase-php) sdk.
composer require kreait/firebase-php After Composer commands run successfully, firebase sdk will be downloaded in vendor folder of laravel project. ( Step 5 completed )
Step 6. Setup Firebase SDK ( kreait/firebase-php ) in laravel and Connect to Firebase
-
a. Rename .json file that you have downloaded from firebase service account to secret.json and paste it in your project public folder.
Your Project public folder will have these files initially, If you are following this guide.
-
b. You need to create factory instance in order to use firebase features like firestore database.
Open web.php file and write following code, it create a new factory instance. You can also create controller but I'm using this for simplicity.
<?php use Illuminate\Support\Facades\Route; use Kreait\Firebase\Factory; Route::get('/', function () { $factory = (new Factory)->withServiceAccount(public_path('secret.json')); }); ?>
Code Explain:
use Kreait\Firebase\Factory; this line will import Factory class from vendor. Its a namespace
secret.json (file that we have downloaded from service account of firebase)
c. By using factory instance , we can create Firestore Database instance and make database opetations.
Following code will create Firestore Database instance.
<?php $firestore = $factory->createFirestore(); $database = $firestore->database(); ?>
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 Laravel
-
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
<?php $reference = $database->getReference('path/to/child/location'); $snapshot = $reference->getSnapshot(); $value = $snapshot->getValue(); print_r($value); ?>
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 :
<?php use Illuminate\Support\Facades\Route; use Kreait\Firebase\Factory; Route::get('/', function () { $factory = (new Factory)->withServiceAccount(public_path('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(); print_r($value); }); ?>
NicheTheme
May 11, 2018 - at 11:00 amFusce ornare mi vel risus porttitor dignissim. Nunc eget risus at ipsum blandit ornare vel sed velit. Proin gravida arcu nisl, a dignissim mauris placerat id. Vivamus interdum urna at sapien varius elementum.
themesflat
May 11, 2018 - at 11:00 amFusce ornare mi vel risus porttitor dignissim. Nunc eget risus at ipsum blandit ornare vel sed velit. Proin gravida arcu nisl, a dignissim mauris placerat id.
NicheTheme
May 11, 2018 - at 11:00 amFusce ornare mi vel risus porttitor dignissim. Nunc eget risus at ipsum blandit ornare vel sed velit. Proin gravida arcu nisl, a dignissim mauris placerat id. Vivamus interdum urna at sapien varius elementum.