7

I'm trying to implement Google's Text-To-Speech API on App Engine flex environment, but get this error:

"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"

Once I solved the problem by requiring BCmath() in my composer.json

{  
    "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"        
    }
}

Then after reinstalling /vendor I can't get rid of the error message. I tried to install the BCmath extension by running

sudo apt install php7.2-bcmath

But it says that the extension is already installed. I run also this php -i | grep -i bcmath And get this message

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

The test for bccomp()

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

I get '-1' as supposed to be. It looks like the function works.

I even tried to enable the BCmath extention in php.ini

extension=bcmath.so

I placed the .ini file in the same directory as my /vendor and index.php. Still, after deployment the app by

gcloud app deploy

I still get the fatal error.

TextToSpeech
  • 251
  • 2
  • 9
  • 1
    Are you sure that bcmath is enabled in your FPM? Check the fpm's php.ini – Mihail0v May 14 '20 at 19:38
  • @Mihail0v Could you please elaborate a bit how to check FPM and enable bcmath there from Google Cloud Shell? – TextToSpeech May 14 '20 at 19:46
  • /etc/php/7.2/fpm/php.ini it supposed to be here by default. Check that file contains "extensions=bcmath.so" section. – Mihail0v May 14 '20 at 20:02
  • @Mihail0v there is no such directory as /fpm. When I'm in /etc/php/7.2/ directory it shows only 'cgi' 'cli' and 'mods-available' subdirectories. In /mods-available there is a bcmath.ini file and its content is "; configuration for php bcmath module ; priority=20 extension=bcmath.so". php.ini is in /cli directory and I didn't find bcmath extesion. So, how to add it ti php.ini? – TextToSpeech May 14 '20 at 20:16
  • @TechToSpeech Sorry, I thought you using php-fpm. You can pass phpinfo();exit; at the begining of index.php to see your available extensions and php.ini path. Does it shows that bcmath is enabled ? – Mihail0v May 14 '20 at 20:19
  • @Mihail0v I found php.ini in /etc/php/7.2/cli directory and bcmath is NOT there among other extensions. So, how I enable it in my php.ini? – TextToSpeech May 14 '20 at 20:23
  • Based on your question, you have already enabled bcmath for 7.2-cli. But It looks like that your index.php runs not by php-cli. So thats why you should try check phpinfo() runned by your index.php and check that it shows bcmath enabled. – Mihail0v May 14 '20 at 20:31
  • @Mihail0v I just run phpinfo(). In the 'Configure Command ' section I found this: '--enable-bcmath=shared'. The php.ini path is /opt/php72/lib/php.ini, but for some reasons I can't enter further than /opt, as Cloud Shell complains that the /opt/php72/lib/ directory doesn't exists. – TextToSpeech May 14 '20 at 21:15
  • You could try https://github.com/phpseclib/bcmath_compat – neubert May 17 '20 at 01:34

2 Answers2

12

After 5days of breaking head, solution was found. It appears that BCMath is installed in PHP versions >= 7, but NOT enabled on Google App Engine. To enable it I did the following:

  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

Then require it in composer.json

{
    "require": {

        "ext-bcmath": "*"  
    }
}

Finally deploy the project

 gcloud app deploy

And that's all!

TextToSpeech
  • 251
  • 2
  • 9
  • Thanks. This issue also been discuss in [Github](https://github.com/protocolbuffers/protobuf/issues/4284). – ibnɘꟻ Aug 28 '20 at 06:57
1

If you get this error from a docker environment, try adding the following to your Dockerfile (it fixed the issue for me).

RUN docker-php-ext-install bcmath

More details here: https://forums.docker.com/t/php-bcmath-extension-doesnt-get-installed-in-dockerfile/99773

Jim Thorpe
  • 11
  • 1