Firebase connection with codeigniter in php
Asked Answered
W

3

5

I want to connect firebase database with my codeigniter project in php.

I not able to find the exact solution and library..

Please recommend the correct library with correct steps that I should follow to connect.

Thanks in advance.

Wraith answered 25/8, 2016 at 4:33 Comment(2)
I am not able to bind library.... Stackoverflow is not for that......Adnah
were you able to find an answer to solve this? I am also in need of the solution for this.Profit
A
5

You can use this library https://github.com/eelkevdbos/firebase-php

Codeigniter Firebase Library

  1. Install kreait/firebase-php package with Composer.
composer require kreait/firebase-php ^4.18
  1. Edit file application/config/autoload.php replace this following line.
...
$autoload['libraries'] = array('firebase');
...
$autoload['config'] = array('firebase');
...
  1. Goto Firebase Console navigate to Project settings -> Service accounts in the Firebase Admin SDK tab, click to the "Generate new private key" button then click "Generate key" to download .json file and save as to application/config folder in the project.

  2. Copy your .json file name from step 3. replace it to application/config/firebase.php file in the $config['filebase_app_key] array like this.

$config['firebase_app_key'] = __DIR__ . '/../config/your-app-firebase-adminsdk-xxxxx-xxxxxxxxxx.json';
  1. Now. You can use firebase library in the Controller file like this.
$this->load->library('firebase');
$firebase = $this->firebase->init();
$db = $firebase->getDatabase();

You can find more usage examples at https://firebase-php.readthedocs.io/en/stable/overview.html#usage-example. and https://www.cloudways.com/blog/php-firebase-integration

Asthenosphere answered 19/8, 2020 at 10:42 Comment(1)
Hi, can you please explain me from step no:4?Swarm
G
1

You can use FireBase in your project by using https://github.com/eelkevdbos/firebase-php

use Firebase\Firebase;

$fb = Firebase::initialize(YOUR_FIREBASE_URL, YOUR_FIREBASE_SECRET);

//or set your own implementation of the ClientInterface as second parameter of the regular constructor
$fb = new Firebase([ 'base_url' => YOUR_FIREBASE_BASE_URL, 'token' => YOUR_FIREBASE_SECRET ], new GuzzleHttp\Client());

//retrieve a node
$nodeGetContent = $fb->get('/node/path');

//set the content of a node
$nodeSetContent = $fb->set('/node/path', array('data' => 'toset'));

//update the content of a node
$nodeUpdateContent = $fb->update('/node/path', array('data' => 'toupdate'));

//delete a node
$nodeDeleteContent = $fb->delete('/node/path');

//push a new item to a node
$nodePushContent = $fb->push('/node/path', array('name' => 'item on list'));
Goshawk answered 25/8, 2016 at 14:33 Comment(1)
Hello, since i am trying to integrate it in codeigniter and i am new for both codeigniter and firebase can you please explain the internal work i should do. Like where to place which file of this package, how to config. PLeaseSubcartilaginous
F
1

Adding to hamza ali's comment.. Do not forget to add following code in your application/libraries folder as "firebase.php"

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

require __DIR__.'/../../vendor/autoload.php';

use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;


class Firebase {

    protected $config   = array();
    protected $serviceAccount;

    public function __construct()
    {
        // Assign the CodeIgniter super-object
        $this->CI =& get_instance();

        $this->serviceAccount = ServiceAccount::fromJsonFile($this->CI->config->item('firebase_app_key'));
    }

    
    public function init()
    {
        return $firebase = (new Factory)->withServiceAccount($this->serviceAccount)->create();
    }
}

Otherwise it will throw an error

Frankhouse answered 12/7, 2022 at 7:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.