我正在尝试使用适用于PHP 7.0的Kraet admin SDK实现Firebase的实时数据库。 我已经安装了composer以及实现所需的所有依赖项,但是我遇到了一个错误,如
**Fatal error: Uncaught Kreait\Firebase\Exception\InvalidArgumentException:
/var/www/html/cabgotel/google-service-account.json could not be parsed to a Service Account:
The following fields are missing/empty in the Service Account specification:
project_id, client_id, client_email, private_key in /var/www/html/cabgotel/vendor/kreait/firebase-php/src/Firebase/ServiceAccount.php:164 Stack trace:
#0 /var/www/html/cabgotel/firebase.php(42): Kreait\Firebase\ServiceAccount::fromJsonFile('/var/www/html/c...')
#1 {main} thrown in /var/www/html/cabgotel/vendor/kreait/firebase-php/src/Firebase/ServiceAccount.php on line 164**
我已经下载了服务帐户json文件,并将其添加到同一目录中。我什至还检查了文件中的值,以了解这是否适用于同一Firebase项目。
以下是我为此使用的代码:
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://www.gstatic.com/firebasejs/5.8.6/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.8.6/firebase.js"></script>
<script>
var config = {
apiKey: "AIzaSyCfGmNSdx5uabtQMGgEjtcz_BL8y_Tm4II",
authDomain: "auth_domain",
databaseURL: "my_db_url",
projectId: "my_project_id",
storageBucket: "my_project_id_storage_bucket",
messagingSenderId: "sender_id"
};
firebase.initializeApp(config);
</script>
</head>
<body>
</body>
</html>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require __DIR__.'/vendor/autoload.php';
use Kreait\Firebase;
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)
->withDatabaseUri('https://my_project.firebaseio.com')
->create();
$database = $firebase->getDatabase();
$newPost = $database
->getReference('https://my_project.firebaseio.com')
->push([
'lan' => '10.77',
'lat' => '88.99'
]);
$newPost->getKey(); // => -KVr5eu8gcTv7_AHb-3-
$newPost->getUri(); // => https://my-project.firebaseio.com/blog/posts/-KVr5eu8gcTv7_AHb-3-
$newPost->getChild('lan')->set('11.211');
$newPost->getValue(); // Fetches the data from the realtime database
$newPost->remove();
请帮助。
答案 0 :(得分:2)
有两个文件,它们完全不同:
google-services.json
用于在您发送给用户的Android应用中使用,并且仅包含Firebase客户端SDK用于在Google服务器上查找项目的配置数据。
服务帐户JSON,其中包含公钥和私钥对,允许代码管理(不受限制)访问您的项目。
向我显示的错误消息类似于代码 可以找到文件,但是 不能找到文件中的某些必填字段。可以确保您从Firebase控制台的“服务帐户”标签中下载服务帐户JSON,如adding Firebase to your app上的文档所示。