How to add Laravel package for the Firebase PHP Admin SDK #4

Learn how to add firebase admin sdk to laravel project - php. In this chapter, we will introduce you to SDK built by Kreait (https://github.com/kreait). Note this is not official SDK by Firebase. But the most used (1.4M downloads and 1.1K Stars) and maintainable code.
Note we are going to use the
Laravel - Firebase package built on above it https://github.com/kreait/laravel-firebase and
Google Firestore PHP SDK  https://github.com/googleapis/google-cloud-php-firestore.
So if you want to start building Laravel app, you can jump to related chapter but understanding base code is always necessary. So lets begin.

The official document url is https://firebase-php.readthedocs.io/ and github url is https://github.com/kreait/firebase-php. The minimum requirement of PHP for this package >= 7.0
mbstring, composer and the firebase json credential file explained in before chapters.

Installation can be done in your PHP project through composer

 composer require kreait/firebase-php ^4.35  


Following the simple code to connect your Firebase to the PHP. Note the below example is for Realtime Database and for Cloud Firestore we have install additional package which we will cover in later part and our main aim would be focusing on Cloud Firestore because in upcoming chapters of Laravel Integration, we will use Cloud Firestore.



Below code is in the core PHP.
 <?php  
 require __DIR__.'/vendor/autoload.php';  
 //This is to load the installed package files from composer  
 use Kreait\Firebase\Factory;  
 //This is main class which connects to your firebase account  
 $factory = (new Factory)  
   ->withServiceAccount('/path/to/google-service-account.json')  
   ->withDatabaseUri('https://my-project.firebaseio.com');  
 //You have to use json file (downloaded on)  
 //And mentioning the database path  
 $database = $factory->createDatabase();  
 //Remember this above line. This is main point you have connected the database.  
 Note: In future chapters of Laravel, the above part would get little change i.e app('firebase.database')  
 $newPost = $database  
   ->getReference('blog/posts')  
   ->push([  
     'title' => 'Post title',  
     'body' => 'This should probably be longer.'  
   ]);  


The above code folder structure would like something as below

Above screenshot uses Windows PC and XAMPP as software. The json file is what you get from Firebase and the above code is inside index.php. Now running this php through http://localhost/firebase/index.php, we get below update in the realtime database of Firebase.


The link mentioned as https://test-cc4d3.firebaseio.com/ is the Database URI you have to provide in the code. (A general reminder: please don't be confused between Realtime Database and Cloud Firestore, This current method is for Realtime Database is for your basic general knowledge, we would be using Cloud Firestore in upcoming chapters which is slight different from this)

Now we are not covering further methods, like fetch, update, delete. If you want, we can have separate chapter for this. So let us know if you want in comment section.

So moving onward, we come to installation of Cloud Firestore, the main part of our further chapter. It requires the below package and a main php extension "grpc" which normal PC or server doesn't contain. We will add a separate chapter for the installation of grpc.

 composer require google/cloud-firestore  

After successfully install, make a slight changes of Cloud Firestore code instead of Realtime database

 <?php  
 require __DIR__.'/vendor/autoload.php';  
 use Kreait\Firebase\Factory;  
 $factory = (new Factory)  
   ->withServiceAccount(/path/to/google-service-account.json);  
 $firestore = $factory->createFirestore();  
 $database = $firestore->database();  
 $collectionReference = $database->collection('blogs');  
 $documentReference = $collectionReference->document('WdY2XvcEyJ94C5noXzYg');  
 $snapshot = $documentReference->snapshot();  
 echo "The name of blog is " . $snapshot['name'];  
The above code is connection the below Firebase > Cloud Firestore



So when you run your code http://localhost/firebase/, the following would be your output


 Now the further methods will be explained in details in the upcoming chapters. Hope you understand the basic usage of Firebase Admin SDK for PHP. In next chapter, we will cover the implementation in the Laravel. So be ready and let us know your thoughts in the comment section below.

Post a Comment

3 Comments

  1. Hello Users if you get issue "error when trying to connect (curl error 60: ssl certificate problem: unable to get local issuer certificate (see http://curl.haxx.se/libcurl/c/libcurl-errors.html))" in your localhost, its due to internet connection. Your PC, localhost must be connected to internet to connect to the Firebase

    ReplyDelete
  2. Hello Users if you get issue "kreait\firebase\exception\invalidargumentexception invalid service account specification" , its probably the json file of Firebase Private key is not in proper position.

    ReplyDelete