Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we use Firebase Realtime database with core PHP?

I have an Android app and backend code is in core PHP using MySql.

Is there any way to perform CRUD operations from API directly the way we perform on MySql?

if it was a web app, it could be done it using javascript, but can we directly from API? Or... Doing it from the Android end would is the last option.

like image 449
parth ravani Avatar asked Feb 22 '18 09:02

parth ravani


1 Answers

To work with Firebase from an Android device, you should definitely use the official Android SDK https://firebase.google.com/docs/android/setup

If you want to execute administrative tasks from a privileged server in PHP, I think you should have a look at https://github.com/kreait/firebase-php - here's a usage example:

<?php

require __DIR__.'/vendor/autoload.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(__DIR__.'/google-service-account.json');

$firebase = (new Factory)
    ->withServiceAccount($serviceAccount)
    ->create();

$database = $firebase->getDatabase();

$newPost = $database
    ->getReference('blog/posts')
    ->push([
        'title' => 'Post title',
        'body' => 'This should probably be longer.'
    ]);

$newPost->getChild('title')->set('Changed post title');
$newPost->getValue(); // Fetches the data from the realtime database
$newPost->remove();

Disclaimer: I am the author of the library :)

like image 88
jeromegamez Avatar answered Sep 30 '22 04:09

jeromegamez