致命错误:未捕获错误:类' Kreait \ Firebase \ ServiceAccount'找不到

时间:2017-12-29 13:39:23

标签: php firebase

我现在必须学习通过fire base编写移动应用程序Web服务。 我点了这个链接:https://firebase-php.readthedocs.io/en/stable/

在我的核心网站中,我创建了web服务文件夹,然后创建了我的fire.php文件。这里的文件代码,

<?php

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

use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;

$serviceAccount = ServiceAccount::fromJsonFile(__DIR__.'/google-service-account.json');
$apiKey = 'AIzaSyC_vb5G9qs3NJsywbR34el1RaPj2HDhwNg';

$firebase = (new Factory)
    ->withServiceAccountAndApiKey($serviceAccount, $apiKey)
    ->withDatabaseUri('https://workarea-cb10b.firebaseio.com')
    ->create();

$database = $firebase->getDatabase();

$newPost = $database
    ->getReference('blog/posts')
    ->push([
        'title' => 'Post title',
        'body' => 'This should probably be longer.'
    ]);

$newPost->getKey(); // => -KVr5eu8gcTv7_AHb-3-
$newPost->getUri(); // => https://my-project.firebaseio.com/blog/posts/-KVr5eu8gcTv7_AHb-3-

$newPost->getChild('title')->set('Changed post title');
$newPost->getValue(); // Fetches the data from the realtime database
$newPost->remove();

我必须在此处致电我的支持文件:https://github.com/kreait/firebase-php/

但我仍然得到了:

  

致命错误:未捕获错误:类&#39; Kreait \ Firebase \ ServiceAccount&#39;   找不到   /opt/lampp/htdocs/workarea/webservice/firebase/fire.php:13堆栈   跟踪:#0 {main}被抛入   第13行/opt/lampp/htdocs/workarea/webservice/firebase/fire.php

问题必须解决这个问题。请检查并帮助我。

我的composer.json文件

{
    "name": "kreait/firebase-php",
    "description": "Firebase Admin SDK",
    "keywords": ["firebase", "google", "sdk", "api", "database"],
    "homepage": "https://github.com/kreait/firebase-php",
    "license": "MIT",
    "authors": [
        { "name": "Jérôme Gamez", "homepage": "https://github.com/jeromegamez" }
    ],
    "require": {
        "php": "^7.0",
        "ext-mbstring": "*",
        "ext-openssl": "*",
        "fig/http-message-util": "^1.1",
        "google/auth": "^0.11.0|^1.0",
        "guzzlehttp/guzzle": "^6.2.1",
        "kreait/firebase-tokens": "^1.1.1",
        "lcobucci/jwt": "^3.2",
        "mtdowling/jmespath.php": "^2.3"
    },
    "require-dev": {
        "friendsofphp/php-cs-fixer": "^2.0",
        "phpstan/phpstan-phpunit": "^0.9.2",
        "phpunit/phpunit": "^6.0"
    },
    "autoload": {
        "psr-4": {
            "Kreait\\": "src"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Kreait\\Tests\\": "tests"
        }
    },
    "config": {
        "platform": {
            "php": "7.0"
        },
        "sort-packages": true
    },
    "extra": {
        "branch-alias": {
            "dev-master": "3.x-dev"
        }
    }
}

3 个答案:

答案 0 :(得分:0)

我确定您已经解决了问题,但是对于新开发的开发人员来说可能会有所帮助, cmd 中使用composer 安装kreait / firebase-php会遇到问题。 > 项目目录中,您的问题将得到解决。

答案 1 :(得分:-1)

我实际上遇到了同样的问题,我通过安装 php-psr exteion 来解决它:

pecl install psr

使用以下几行创建/etc/php/7.x/mods-available/psr.ini:

; configuration for psr module
; priority=20
 extension=psr.so

并通过执行启用扩展:

sudo phpenmod psr

并重新加载/重启apache、php-fpm.....

service apache2 restart
service php7.4-fpm restart

答案 2 :(得分:-2)

我建议从Firebase Installation开始。在那里描述了过程,但重点是:

使用Composer在命令行中安装firebase-php软件包:

php composer.phar require kreait/firebase-php

这应该解决缺少课程的问题。