parth ravani
parth ravani

Reputation: 71

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.

Upvotes: 5

Views: 13396

Answers (1)

jeromegamez
jeromegamez

Reputation: 3551

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 :)

Upvotes: 12

Related Questions