You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP Firestore项目部署Heroku:无gRPC扩展的使用方案咨询

无gRPC环境下在Heroku部署PHP Firestore应用的解决方案

以下是几个可行的方案,帮你绕开gRPC扩展限制完成部署:

1. 直接调用Firestore REST API

Firestore官方提供完整的REST接口,无需依赖gRPC扩展。你可以用PHP的curl或Guzzle等HTTP库直接发送请求,核心是通过服务账号生成认证令牌来鉴权。

简单示例(获取文档):

<?php
use Firebase\JWT\JWT;
use GuzzleHttp\Client;

// 加载服务账号密钥
$serviceAccount = json_decode(file_get_contents(__DIR__.'/service-account.json'), true);

// 生成JWT令牌
$now = time();
$token = [
    'iss' => $serviceAccount['client_email'],
    'sub' => $serviceAccount['client_email'],
    'aud' => 'https://firestore.googleapis.com/',
    'iat' => $now,
    'exp' => $now + 3600, // 1小时有效期
];

$jwt = JWT::encode($token, $serviceAccount['private_key'], 'RS256');

// 发送请求获取文档
$client = new Client();
$response = $client->get('https://firestore.googleapis.com/v1/projects/your-project-id/databases/(default)/documents/your-collection/your-document', [
    'headers' => [
        'Authorization' => 'Bearer '.$jwt,
        'Content-Type' => 'application/json',
    ],
]);

$document = json_decode($response->getBody(), true);

2. 自定义构建流程在Heroku安装gRPC扩展

Heroku并非完全无法安装gRPC,你可以通过自定义构建步骤来编译安装:

步骤:

  • 在composer.json中添加ext-grpc到require字段:
    {
        "require": {
            "ext-grpc": "*",
            "firebase/php-jwt": "^6.0",
            "guzzlehttp/guzzle": "^7.0"
        }
    }
    
  • 添加heroku-buildpack-apt作为前置buildpack,用来安装编译依赖:
    heroku buildpacks:add --index 1 heroku-community/apt
    
  • 在项目根目录创建Aptfile,添加编译所需依赖:
    build-essential
    autoconf
    libssl-dev
    
  • 在composer.json中添加post-install脚本自动安装gRPC:
    "scripts": {
        "post-install-cmd": [
            "pecl install grpc"
        ]
    }
    
  • 部署时Heroku会自动执行这些步骤,完成gRPC扩展的编译安装。

3. 使用支持REST的Firebase SDK封装库

部分社区维护的Firebase PHP SDK支持切换到HTTP客户端,无需gRPC。比如kreait/firebase-php可以通过配置禁用gRPC:

示例代码:

<?php
use Kreait\Firebase\Factory;
use Kreait\Firebase\Http\GuzzleHttpClient;

$factory = (new Factory)
    ->withServiceAccount(__DIR__.'/service-account.json')
    // 强制使用HTTP客户端替代gRPC
    ->withHttpClient(new GuzzleHttpClient());

$firestore = $factory->createFirestore();

// 正常使用Firestore API,比如获取文档
$document = $firestore->database()->collection('your-collection')->document('your-document')->snapshot();

注意:使用该库时,确保安装的版本支持HTTP客户端模式,建议查看最新文档确认配置细节。

内容的提问来源于stack exchange,提问作者Anthony Rafael

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 05:16:14