PHP连接Firestore失败:Google ApiCore Serializer类未找到,求解决方案
问题详情
- 已参考Linux下gRPC安装指南和Firestore PHP快速入门指南操作
- 测试代码:
<?php require __DIR__ . '/vendor/autoload.php'; use Google\Cloud\Firestore\FirestoreClient; $firestore = new FirestoreClient([ 'keyFilePath' => '/var/www/path/to/my/key.json', 'projectId' => 'test-app', ]);
- 当前Composer配置:
{ "require": { "grpc/grpc": "^1.38", "google/cloud-firestore": "^0.1.0", "google/apiclient": "^2.12.1", "google/cloud-core": "^1.47" } }
- 错误日志:
> /var/log/apache2/error.log <
[Tue Oct 04 02:47:33.613513 2022] [php7:error] [pid 2826323] [client my.IP.address] PHP Fatal error: Uncaught Error: Class 'Google\ApiCore\Serializer' not found in /var/www/html/vendor/google/cloud-core/src/GrpcRequestWrapper.php:93
Stack trace:
#0 /var/www/html/vendor/google/cloud-firestore/Connection/Grpc.php(81): Google\Cloud\Core\GrpcRequestWrapper->__construct()
#1 /var/www/html/vendor/google/cloud-firestore/FirestoreClient.php(111): Google\Cloud\Firestore\Connection\Grpc->__construct()
#2 /var/www/html/v.php(8): Google\Cloud\Firestore\FirestoreClient->__construct()
#3 {main}
thrown in /var/www/html/vendor/google/cloud-core/src/GrpcRequestWrapper.php on line 93
解决步骤
更新Firestore客户端版本
你使用的google/cloud-firestore版本是^0.1.0,属于极早期测试版本,依赖关系混乱且缺少必要组件,需替换为稳定版(比如^1.40或更高)。简化Composer依赖配置
无需手动指定grpc/grpc、google/cloud-core等依赖,google/cloud-firestore会自动拉取所有兼容的必要组件。修改composer.json为:
{ "require": { "google/cloud-firestore": "^1.40" } }
- 重新安装依赖
执行以下命令更新并安装正确的依赖包:
composer update
确保命令执行无报错,所有依赖成功安装。
- 验证gRPC扩展是否启用
检查PHP配置文件php.ini,确认已添加extension=grpc.so(Linux环境),随后重启Apache服务。可通过php -m命令查看是否存在grpc模块,确认扩展生效。
完成以上步骤后,重新运行测试代码即可正常连接Firestore。
内容的提问来源于stack exchange,提问作者Petro

