Dominik Grzelak blog

This short how-to shows you how you can read values stored in the firebase database within your web app using php.
For that we'll be using the library firebase-php from kreait.

Library for PHP 5.5/5.6 support

https://github.com/kreait/firebase-php/tree/1.x

PHP 7

https://github.com/kreait/firebase-php

This library is a PHP client for the Google Firebase Database using the Google API.

For the authentication we're going to use a Google Service Account.

Service account credentials

First, open your firebase project in the firebase console. Once opened click at the small gear icon beside Overview (Settings) -> Project settings and then select the service account tab.

open-settings_474089421.jpg

The service account Firebase Admin SDK is pre-selected. At the bottom you can click at the button "Create new private key".

The same can be done this way:
Again click in your overview page of your project at the small gear icon and select Permissions. In the left sidebar select the entry Service accounts. Now you can create your key for the service account with the name firebase-adminsdk. For that you have to click Options (three small vertical dots) on the right-hand side of the entry in the table:

create-key-2_2384396600.png

Here you will also see a list of keys created before.

In both ways you have to download a json file. It cannot be restored so you have to store it at a secure place, or generate a new key if the old one gets lost.

Set permissions
If you only want to read values then you are fine for now. But if you want to write data to firebase database you have to change the role to editor. At the IAM page (see the later approach) select the the service account firebase-adminsdk and then at the top select Permissions. A sidebar will open on the right-hand side. There you can change the role of your service account.

Install firebase-php

Include the Kreait library in your php project with composer or cloning the git repository mentioned above. Use the version with correct php support.

Usage

To read data:

include_once "../../vendor/autoload.php";
use Kreait\Firebase\Configuration;
use Kreait\Firebase\Firebase;

$config = new Configuration();
$config->setAuthConfigFile("google-service-account-key.json"); //authentication with google service account
$firebase = new Firebase("https://.firebaseio.com", $config);

$usernames = $firebase->get('usernames');

Here we're using the convenience method get() to directly access the data.

With reference it looks like this:

$firebase->getReference("usernames")->getData();

More complexe queries can be constructed by using the reference to filter and order the data:

$query = new Query();
$query->orderByKey()->limitToLast(10);
$result = $firebase->getReference("usernames")->query($query);

First, we construct the query and then we execute it on a firebase database reference.

Have a look at the documentation for topics like other authentication methods or writing data:
Version 1.x: https://firebase-php.readthedocs.io/en/1.x/
Latest Version: https://firebase-php.readthedocs.io/en/latest/

Errors

Fatal error: Uncaught exception 'GuzzleHttp\Exception\RequestException' with message 'cURL error 60: SSL certificate problem: unable to get local issuer certificate (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)'

Solution: https://stackoverflow.com/questions/21114371/php-curl-error-code-60#21114601.Dont forget to restart the web server afterwards.

If the error still occurs try this old certificate bundle from https://github.com/bagder/ca-bundle.

Fatal error: Uncaught exception 'Kreait\Firebase\Exception\PermissionDeniedException' with message 'Server error (401) for URL https://offbeat-math.firebaseio.com/usernames.json with data "": Unauthorized request.'

Check if you're using the correct json key file for your project.