Fatal error: Uncaught Error: Call to undefined method Google\Cloud\Storage\StorageClient::collection()

Fatal error: Uncaught Error: Call to undefined method Google\Cloud\Storage\StorageClient::collection()

我是这个 Firestore PHP 库的新用户。所以我尝试通过 Composer 使用命令安装 Google 库:

$ composer require google/cloud-firestore

$ composer require google/cloud

$ composer require google/cloud-storage

然后我在 XAMPP 上安装了 gRPC,并按照 link 上的说明进行操作: Google Cloud PHP Authentication。 之后,我按照这条指令的示例进行操作: Google Cloud PHP Sample

但它 returns 像这样的错误

Fatal error: Uncaught Error: Call to undefined method Google\Cloud\Storage\StorageClient::collection() in C:\xampp\htdocs\phpfirebasecomposer\firestore.php:3 Stack trace: #0 {main} thrown in C:\xampp\htdocs\phpfirebasecomposer\firestore.php on line 3

这是我的 dbcon.php,我用它来存储凭据和身份验证尝试

<?php

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

use Kreait\Firebase\Factory;
use Kreait\Firebase\Auth;
use Google\Cloud\Firestore\FirestoreClient;
use Google\Cloud\Storage\StorageClient;

// Authenticating with keyfile data.
$firestore = new FirestoreClient([
 'projectId' => 'myid',
]);

// Authenticating with a keyfile path.
$firestore = new StorageClient([
 'keyFilePath' => 'my.json'
]);

$firestore = new StorageClient([
 'keyFile' => json_decode(file_get_contents('my.json'), true)
]);

$factory = (new Factory)
->withServiceAccount('my.json')
->withDatabaseUri('dblink');

$database = $factory->createDatabase();
$auth = $factory->createAuth();

?>

这是我用来回显所需数据的 firestore.php

<?php
 include 'dbcon.php';
 $collectionReference = $firestore->collection('users');
 $documentReference = $collectionReference->document('a4Wq47gPeqY3UjeGbR40ptPZ0sq2');
 $snapshot = $documentReference->snapshot();
 echo "Hello " . $snapshot['uid'];
?>

我想问的是,未定义方法可能是什么问题?谢谢

正如错误信息所指出的:

Fatal error: Uncaught Error: Call to undefined method Google\Cloud\Storage\StorageClient::collection()

StorageClient 中没有 collection() 方法。您已多次声明 $firestore 变量导致该错误。

在这行代码中,必须使用另一个变量名给StorageClient:

$firestore = new FirestoreClient([
 'projectId' => 'myid',
]);

// This line replaces the instance of Firestore
// $firestore now uses an instance of StorageClient
$firestore = new StorageClient([
 'keyFilePath' => 'my.json'
]);

** Firebase PHP SDK **

Firebase Admin PHP SDK 在 Packagist 上可用 kreait/firebase-php: 尝试通过 Composer 安装它,您可以将所有 firebase 管理员与 PHP

一起使用
composer require kreait/firebase-php

对于 cloud firestore,您需要在 SDK 中安装该 googlecloud firestore

composer require google/cloud-firestore

**这是我的配置示例**

        ini_set('display_errors', 1);
        
        require __DIR__ . '/vendor/autoload.php';
        
        use Kreait\Firebase\Factory;
        use Google\Cloud\Firestore\FirestoreClient;
        use Google\Cloud\Storage\StorageClient;
        
        $factory = (new Factory)
            ->withServiceAccount('../config/my-parbhani-firedddbase-adminsdk-bztm1-d6debd5f64.json') // Add the service_accounts.json
            ->withDatabaseUri('https://default-rtdb.firebaseio.com/'); // add the database uri
        
        $database = $factory->createDatabase();
        $auth = $factory->createAuth();
        $firestore = new FirestoreClient([
       'projectId' => 'my-parbhani-firedddbase', // project id
        ]);

        $firestore = new StorageClient([
     'keyFilePath' => 'my-parbhani-firedddbase-adminsdk-bztm1-d6debd5f64.json' // Add the service_accounts.json
    ]);