Home > front end >  Fatal error: Uncaught Error: Call to undefined method Google\Cloud\Storage\StorageClient::collect
Fatal error: Uncaught Error: Call to undefined method Google\Cloud\Storage\StorageClient::collect

Time:06-04

I'm a new one to this Firestore PHP library. So I tried to install the Google Library via Composer with command :

$ composer require google/cloud-firestore

and

$ composer require google/cloud

and

$ composer require google/cloud-storage

Then I installed gRPC on my XAMPP, and follow the instruction on this link : Google Cloud PHP Authentication. After that, I follow a sample on this instruction : Google Cloud PHP Sample

But it returns an error like this

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

This is my dbcon.php, which I used to store the credential and authentication attempts

<?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();

?>

And this is the firestore.php that I used to echo the desired data

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

What I'm asking is, what could possibly be the problem of undefined method ? Thanks

CodePudding user response:

As pointed out by the error message:

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

There's no collection() method in StorageClient. You have declared the $firestore variable multiple times which causes that error.

In this line of code, you must use another variable name to the 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'
]);

CodePudding user response:

** Firebase PHP SDK **

The Firebase Admin PHP SDK is available on Packagist as kreait/firebase-php: Try to install it via Composer you can use all the firebase admin with PHP

composer require kreait/firebase-php

for cloud firestore you will need to install that googlecloud firestore in the SDK

composer require google/cloud-firestore

**here's an example my config **

        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
    ]);
    
  
  • Related