phpfirebase-cloud-messaginggoogle-admin-sdk

FCM with Php and Admin-sdk. Shows firebase sdk class not found


I used composer to install firebase admin-sdk with command composer require kreait/firebase-php The resulting downloaded folder structure had

vendor/kreait/firebase-php , vendor/kreait/clock, vendor/kreait/firebase-tokens.

When I ran composer show kreait/firebase-php it did not list firebase-php under the require section. Instead kreait/firebase-tokens and kreait/clock was listed. the version for admin-sdk is 5.26.5. The vendor folder is in the root of my project folder. I added a test.php file with the following code.

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

if (class_exists('Kreait\Firebase\Firebase')) {
    echo "Firebase class found!";
    $firebase = new \Kreait\Firebase\Firebase(); // Or your Firebase initialization code
} else {
    echo "Firebase class NOT found!";
} 

It outputs class not found. Output for php -v is

PHP 8.3.14 (cli) (built: Nov 19 2024 15:53:36) (NTS Visual C++ 2019 x64)
Copyright (c) The PHP Group
Zend Engine v4.3.14, Copyright (c) Zend Technologies

I next tried composer clear-cache composer install. Still it cannot find the sdk location when i run the test.php. There is something wrong with whatever being downloaded by the composer. The folder structure is not the way it should be. Please advise.


Solution

  • Ensure the correct class is being referenced. use Kreait\Firebase\Factory to initialize Firebase. Modify your code like this:

    require __DIR__ . '/vendor/autoload.php';
    use Kreait\Firebase\Factory;
    $firebase = (new Factory)->create();
    echo "Firebase initialized!";
    

    check that the composer.json file lists the package correctly and try clearing the Composer cache