此错误与Firebase版本有关吗?如果没有,我该如何解决这个问题?
private static function fromJsonFile(string $filePath): self
{
try {
$file = new \SplFileObject($filePath);
$json = (string) $file->fread($file->getSize());
} catch (Throwable $e) {
throw new InvalidArgumentException("{$filePath} can not be read: {$e->getMessage()}");
}
try {
$serviceAccount = self::fromJson($json);
} catch (Throwable $e) {
throw new InvalidArgumentException(\sprintf('%s could not be parsed to a Service Account: %s', $filePath, $e->getMessage()));
}
return $serviceAccount;
}
答案 0 :(得分:0)
您发布的代码来自kreait/firebase-php,并显示了ServiceAccount
类的私有方法,该方法自SDK 5.0版以来就不能直接调用。
直接从troubleshooting section in the documentation:
您可能遵循了针对4.x版本的教程文章或视频,并且您的代码如下所示:
use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;
$serviceAccount = ServiceAccount::fromJsonFile(__DIR__.'/google-service-account.json');
$firebase = (new Factory)
->withServiceAccount($serviceAccount)
->create();
$database = $firebase->getDatabase();
将其更改为以下内容:
use Kreait\Firebase\Factory;
$factory = (new Factory)->withServiceAccount(__DIR__.'/google-service-account.json');
$database = $factory->createDatabase();