Firebase SDK available most of the client platform still it’s not available some cross-platform like xamarin, you can easily firebase access through PHP using the below steps.
Requirements
- PHP >= 7.0
- The mbstring PHP extension
- A Firebase project – create a new project in the Firebase console
- google-service-account.json
- Database URL
Firebase PHP SDK
The recommended way to install the Firebase Admin SDK is with Composer
1 2 3 4 5 |
composer require kreait/firebase-php ^4.0 |
OR
1 2 3 4 5 6 7 8 9 |
{ "require": { "kreait/firebase-php": "^4.0" } } |
OR
JSON_PATH and FIREBASE_URL update as per your project credential.
Real-time Database API
Get database table data
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
require __DIR__.'/vendor/autoload.php'; require __DIR__.'/config.php'; use Kreait\Firebase\Factory; use Kreait\Firebase\ServiceAccount; // This assumes that you have placed the Firebase credentials in the same directory // as this PHP file. $serviceAccount = ServiceAccount::fromJsonFile(JSON_PATH); $firebase = (new Factory) ->withServiceAccount($serviceAccount) // The following line is optional if the project id in your credentials file // is identical to the subdomain of your Firebase project. If you need it, // make sure to replace the URL with the URL of your project. ->withDatabaseUri(FIREBASE_URL) ->create(); $database = $firebase->getDatabase(); $reference = $database->getReference('users'); // $snapshot = $reference->getSnapshot(); $users = $reference->getValue(); |
Create a new user inside authorize and table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
require __DIR__.'/vendor/autoload.php'; require __DIR__.'/config.php'; use Kreait\Firebase\Factory; use Kreait\Firebase\ServiceAccount; // This assumes that you have placed the Firebase credentials in the same directory // as this PHP file. $serviceAccount = ServiceAccount::fromJsonFile(JSON_PATH); $firebase = (new Factory) ->withServiceAccount($serviceAccount) // The following line is optional if the project id in your credentials file // is identical to the subdomain of your Firebase project. If you need it, // make sure to replace the URL with the URL of your project. ->withDatabaseUri(FIREBASE_URL) ->create(); $auth = $firebase->getAuth(); $user=array(); try { $userProperties = [ 'email' =>'abc@gamil.com', 'emailVerified' => false, 'password' => 'passwordnew', 'displayName' => 'WeAnswer', 'disabled' => false, ]; $user = $auth->createUser($userProperties); $database = $firebase->getDatabase(); $newPost = $database->getReference('users/'.$user->uid)->set([ 'userId'=>$user->uid, 'email' =>'abc@gamil.com', 'password' => 'passwordnew', 'displayName' => 'WeAnswer', ]); } catch (Exception $e) { $msg=$e->getMessage(); } |
Update record
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
require __DIR__.'/vendor/autoload.php'; require __DIR__.'/config.php'; use Kreait\Firebase\Factory; use Kreait\Firebase\ServiceAccount; // This assumes that you have placed the Firebase credentials in the same directory // as this PHP file. $serviceAccount = ServiceAccount::fromJsonFile(JSON_PATH); $firebase = (new Factory) ->withServiceAccount($serviceAccount) // The following line is optional if the project id in your credentials file // is identical to the subdomain of your Firebase project. If you need it, // make sure to replace the URL with the URL of your project. ->withDatabaseUri(FIREBASE_URL) ->create(); $auth = $firebase->getAuth(); $database = $firebase->getDatabase(); $newPost=array(); try { $newPost = $database->getReference('users/sdpecmajndpdncsalm]); $newPost->getChild('distance')->set("132"); $newPost->getChild('comment')->set("Love"); } catch (Exception $e) { $msg=$e->getMessage(); } |
More Stories
How to set EC2 Ubuntu default password
CPU & Memory usage in PHP
Install PHP mcrypt extension on Ubuntu