This SDK makes it easy to interact with Google Firebase applications.
- Simple and fluent interface to work with References, Querys and Data snapshots
- Abstracts away the underlying communication with the Firebase REST API
- Supports authentication with a Google service account (V3) or a database secret (V2)
- Removes limitations of the REST API (e.g. sorted results)
Starting with version 2.0, this SDK requires PHP 7 - for PHP 5.5/5.6 support, please use Version 1.x.
The recommended way to install the Firebase SDK is with Composer. Composer is a dependency management tool for PHP that allows you to declare the dependencies your project needs and installs them into your project.
# Install Composer
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"
You can add the Firebase SDK as a dependency using the composer.phar CLI:
php composer.phar require kreait/firebase-php ^2.0@beta
The @beta
version constraint is only needed until the documentation is completed.
Alternatively, you can specify the Firebase SDK as a dependency in your project's existing composer.json file:
{
"require": {
"kreait/firebase-php": "^2.0@beta"
}
}
After installing, you need to require Composer's autoloader:
require 'vendor/autoload.php';
You can find out more on how to install Composer, configure autoloading, and other best-practices for defining dependencies at getcomposer.org.
Create a service account as described in the Firebase Docs and download the service account JSON file, or retrieve a database secret from your Firebase application's project settings page.
$firebase = Firebase::fromServiceAccount(__DIR__.'/google-service-account.json');
$database = $firebase->getDatabase();
$root = $database->getReference('/');
$completeSnapshot = $root->getSnapshot();
$root->getChild('users')->push([
'username' => uniqid('user', true),
'email' => uniqid('email', true).'@domain.tld'
]);
$users = $database->getReference('users');
$sortedUsers = $users
->orderByChild('username', SORT_DESC)
->limitToFirst(10)
->getValue(); // shortcut for ->getSnapshot()->getValue()
$users->remove();
The documentation is not complete yet - the SDK will stay in beta until the docs at http://firebase-php.readthedocs.io are finished.
Please feel free to open an issue in this repository if something is unclear - but if your IDE supports autocompletion, you should be fine :).
- Integration of the Firebase Storage
- Automatic updates of Firebase Rules
- Support for PHP Object Serialization/Deserialization
- Listening to the Firebase event stream