1
votes

I am new to Firebase/Firestore. I have created 2 projects that access the same data in Firestore. One is in android and the other a web app in php. I am able to access data in android app but in PHP app i had to create an API key in the credentials page. I want to use Firestore on trial basis initially but am not able to access the documents . It gives an error

Fatal error: Uncaught RuntimeException: Credentials fetcher does not implement Google\Auth\UpdateMetadataInterface in C:\xampp\htdocs\FirestoreProject\vendor\google\auth\src\FetchAuthTokenCache.php:190 Stack trace: #0 C:\xampp\htdocs\FirestoreProject\vendor\google\gax\src\CredentialsWrapper.php(197): Google\Auth\FetchAuthTokenCache->updateMetadata(Array, 'https://firesto...') #1 [internal function]: Google\ApiCore\CredentialsWrapper->Google\ApiCore{closure}(Object(stdClass)) #2 C:\xampp\htdocs\FirestoreProject\vendor\grpc\grpc\src\lib\ServerStreamingCall.php(44): Grpc\Call->startBatch(Array) #3 C:\xampp\htdocs\FirestoreProject\vendor\grpc\grpc\src\lib\BaseStub.php(364): Grpc\ServerStreamingCall->start(Object(Google\Cloud\Firestore\V1\BatchGetDocumentsRequest), Array, Array) #4 C:\xampp\htdocs\FirestoreProject\vendor\grpc\grpc\src\lib\BaseStub.php(589): Grpc\BaseStub->Grpc{closure}('/google.firesto...', Object(Google\Cloud\Firestore\V1\BatchGetDocumentsRequest), Array, Array, Array) #5 C:\xampp\htdocs\FirestoreProject\ve in C:\xampp\htdocs\FirestoreProject\vendor\google\auth\src\FetchAuthTokenCache.php on line 190

I have installed composer and updated the dependencies and gRPC.

My index.php file reads

<?php
    session_start();
    require_once 'vendor/autoload.php';
    require_once "Firestore.php";
    
    
    $apiKey = 'WWWWWWWWWWWWWWWWWWWWWW';
    
    
    $url ='https://console.firebase.google.com/project/prject-ID/firestore? 
    key=Aasdads';
    $fs = new Firestore('Users');
    print_r($fs->getDocument('9427349073'));
    
    return;
?>

My Firestore.php file reads

<?php

    use Google\Cloud\Core\Exception\GoogleException;
    use Google\Cloud\Firestore\FirestoreClient;
    
    class Firestore
    {
    
        protected $db;
        protected $name;   // $name is name of collection
    
        public function __construct(string $collection) // $collection = 
                                                        //  'Users'
        {
            if (isset($this)) {
    
                try {
                    $this->db = new FirestoreClient([
                    'projectId' => 'XXXXXXXXXXXXXXXXXXX'
                    ]);
     
                    $this->name = $collection;
    
                } catch (GoogleException $e) {
                    print_r($e->getMessage());
                }
    
            }else{
    
    
            }


    }


    public function getDocument(string $name)  // $name is name of document 
    {
        return  $this->db->collection($this->name)->document($name)- 
        >snapshot()->data();

    }

}


?>

I tried activating the Credentials Page but they are asking for Credit card for billing to start trial , which i dont have. Cant we test the database without activating the Credentials? What is the difference between testing and trial basis usage ?

Please help

regards

Sanjish

I have made a test.php file after creating a service account and pointing to the key.json file on command prompt using set GOOGLE_APPLICATION_CREDENTIALS

<?php

    require_once 'vendor/autoload.php';
    
    session_start();
    
    use Google\Cloud\Firestore\FirestoreClient;
  
     initialize();
    
    
    function initialize()
    {
        // Create the Cloud Firestore client
    
        $db = new FirestoreClient([
            'projectId' => 'Valbhai-xxx'
        ]);
    
      
        $usersRef = $db->collection('Users'); // This works
        $snapshot = $usersRef->documents();   // This does not work , gives 
                                              // the same error
    
        foreach ($snapshot as $user) {
    
            printf('User: %s' . PHP_EOL, $user->client_name());
        }
    
    }

?>
1
Hi have you defined your credentials as is mentioned in the PHP firestore quickstart, PHP is a backend software and it is necessary to use the service account credentials to use Firestore - Jan Hernandez
I followed your advice Jan , and created a service account .It worked. Thanks a lot - SANJISH KUMAR
It worked partially . $usersRef = $db->collection('Users'); -> This works but $doc = $usersRef->documents(); gives the same error - SANJISH KUMAR
Can you update the code in your question to understand what works and what doesn't? - Jan Hernandez

1 Answers

2
votes

I reproduced your issue on my computer and I noticed that this is because PHP is ignoring the environment variable GOOGLE_APPLICATION_CREDENTIALS, because this is not defined for the user Apache(or httpd) in order to fix this I modified the initialization by using this code block


$db = new FirestoreClient([
            'keyFilePath' => '/path/to/credentials.json',
            'projectId' => 'my-project-id'
        ]);

With keyFilePath (this parameter is documented here), Firestore library going to search the GCP credentials using the string of the path instead the environment variable