Max Webb
Max Webb

Reputation: 152

PHP Call to private method Kreait\Firebase\ServiceAccount::fromJsonFile() error

I seem to be having an issue with the Kreait Library for using Firebase in PHP rather than JS.

Running this code, it returns this error

Fatal error: Uncaught Error: Call to private method Kreait\Firebase\ServiceAccount::fromJsonFile() from context '' in /Users//Documents/GitHub/phpvers/index.php:15
Stack trace:
#0 {main}
  thrown in /Users//Documents/GitHub/phpvers/index.php on line 15

Have looked at similar questions, but does not seem to relate to my issue.

Full code is below:

<?php
/**
 * Created by PhpStorm.
 * User: SYSTEM
 * Date: 18-Jul-18
 * Time: 22:15
 */

require_once './vendor/autoload.php';

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


$serviceAccount = ServiceAccount::fromJsonFile(__DIR__.'/shit/file.json');

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

$database = $firebase->getDatabase();

die(print_r($database)); 

I have copied and pasted this to try and see if it was my code that wasn't working but still seems to be having issues.

The GitHub repository is located here: https://github.com/arthimann/firebase-php/blob/master/index.php

I am using the Firebase SDK, which documentation is located here: https://firebase-php.readthedocs.io/

Thanks, Max.

Upvotes: 3

Views: 6938

Answers (1)

Max Webb
Max Webb

Reputation: 152

Fixed this by doing more research into the issue! Always the way isn't it :P

I changed this code:

$serviceAccount = ServiceAccount::fromJsonFile(__DIR__.'/shit/file.json');

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

$database = $firebase->getDatabase();

To the following:

$factory = (new Factory)
     ->withServiceAccount(__DIR__.'/google-service-account.json')
     ->withDatabaseUri('https://ur/database/url/firebasedatabase.app');

$database = $factory->createDatabase();

Upvotes: 12

Related Questions