最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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.

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.

Share Improve this question asked Feb 2 at 9:09 adburyadbury 518 bronze badges 2
  • 2 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 – Kamyar Safari Commented Feb 3 at 6:43
  • 1 Thank you. This was the correct way. A new Factory() instance was to be created and not a new firebase() like I was doing. I cannot mark your comment as the accepted answer. If you have the time please post it as answer. – adbury Commented Feb 14 at 23:33
Add a comment  | 

1 Answer 1

Reset to default 1

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

发布评论

评论列表(0)

  1. 暂无评论