Can't figure out why I get App Engine flex "Uncaught Error: Call to undefined function Google\Protobuf\Internal\bccomp()"

Can't figure out why I get App Engine flex "Uncaught Error: Call to undefined function Google\Protobuf\Internal\bccomp()"

我正在尝试在 App Engine flex 环境中实现 Google 的文本转语音 API,但出现此错误:

"PHP message: PHP Fatal error: Uncaught Error: Call to undefined function Google\Protobuf\Internal\bccomp() in /app/web/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php:941"

一旦我通过在 composer.json

中要求 BCmath() 解决了问题
{  
    "require": {
        "ext-bcmath": "*",
        "google/cloud-text-to-speech": "^1.0",
        "google/gax": "^1.3",
         "grpc/grpc": "^1.4",
         "google/auth": "^1.8",
         "phpseclib/phpseclib": "^2.0",
         "google/protobuf": "^3.11"        
    }
}

然后在重新安装 /vendor 后我无法摆脱错误信息。我试图通过 运行ning

安装 BCmath 扩展
sudo apt install php7.2-bcmath

但是它说扩展已经安装了。 我运行也是这个 php-我 | grep -i bcmath 并收到此消息

/etc/php/7.2/cli/conf.d/20-bcmath.ini, bcmath BCMath support => enabled bcmath.scale => 0 => 0

bccomp() 测试

php -r "echo bccomp('1', '2');"

我得到的是“-1”。看起来功能有效。

我什至尝试在 php.ini

中启用 BCmath 扩展
extension=bcmath.so

我将 .ini 文件放在与我的 /vendor 和 index.php 相同的目录中。尽管如此,在

部署应用程序后
gcloud app deploy

我仍然遇到致命错误。

经过5天的破头,终于找到解决办法了。 BCMath 似乎安装在 PHP 版本 >= 7 中,但未在 Google App Engine 上启用。为了启用它,我做了以下操作:

  1. I have created php.ini file and placed it in the same directory as app.yaml file, which might be different from the root directory of your app (index.php for example).
  2. In empty newly created php.ini have added one line:
extension=bcmath

然后在composer.json

中要求它
{
    "require": {

        "ext-bcmath": "*"  
    }
}

最后部署项目

 gcloud app deploy

仅此而已!