为什么在 App Engine 上尝试创建 Google 的 class TextToSpeechClient 实例时出现服务器致命错误?

Why do I get a server fatal error when trying to create an instance of Google's class TextToSpeechClient on App Engine?

我在 App Engine 弹性环境中部署 PHP 应用程序时尝试创建 Googles 的 TextToSpeechClient class 实例时遇到致命的服务器错误。在本地主机上,它可以正常工作。以下是错误信息:

"NOTICE: PHP message: PHP Fatal error: Uncaught Error: Class 'Google\Cloud\TextToSpeech\V1\TextToSpeechClient' not found in /app/web/get_voices2.php:46"

我的get_voices2.php

<?php
// includes the autoloader for libraries installed with composer

require __DIR__ . '/vendor/autoload.php';
require_once('includes/dbPDO.php');

// Imports the Cloud Client Library

use Google\Cloud\TextToSpeech\V1\AudioConfig;
use Google\Cloud\TextToSpeech\V1\AudioEncoding;
use Google\Cloud\TextToSpeech\V1\SsmlVoiceGender;
use Google\Cloud\TextToSpeech\V1\SynthesisInput;
use Google\Cloud\TextToSpeech\V1\TextToSpeechClient;
use Google\Cloud\TextToSpeech\V1\VoiceSelectionParams;
use Google\Cloud\Storage\StorageClient;


if (isset($_POST['language']) && isset($_POST['quality'])) {


        $storage = new StorageClient();
        $language = $_POST['language'];
        $quality = $_POST['quality'];
        $dsn = getenv('MYSQL_DSN');
        $user = getenv('MYSQL_USER');
        $password = getenv('MYSQL_PASSWORD');
        $dbh = OpenCon($dsn,$user,$password);

    echo getListVoices($language, $quality, $dbh);
}


function getListVoices($lan, $quality,$conn) {
    $optionData = '<option id = "0" disabled>Select voice</option>';

    // instantiates a client on line 46
    $client = new TextToSpeechClient(['credentials' => json_decode(file_get_contents('cred.json'), true)]);

    $response = $client->listVoices();
    $voices = $response->getVoices();
 } 

这是我的 App Engine 文件夹结构。请注意 app.yaml 文件不在 web 目录中。它与 /various 和 /php-docs-sample 在同一个目录中 PHP app's web directory structure

我的 composer.json 文件:

{
    "require": {
        "google/cloud-speech": "^1.0.1",
        "google/gax": "^1.1",
         "grpc/grpc": "^1.4",
         "google/protobuf": "^v3.3.0",
        "google/auth": "^1.8",
        "phpseclib/phpseclib": "^2.0"
    }
}

我通过 运行 命令在 App Engine 上部署我的项目:

gcloud app deploy -version dev

希望我提供的信息完整。

根据 repository,包 google/cloud-text-to-speech 中提供了 class - 但根据您的 composer.json,您不需要该包。

为什么你在 require-dev 部分需要 google/cloud-core?这是一个好兆头,表明您在开发系统中使用了一组与生产系统不同的特定于应用程序的 classes。通常,这应该只包括属于您开发的一部分的东西(例如:调试工具、测试工具),而不是那些提供应用程序基础的东西

我只是用一个有效的解决方案来跟进我的问题。正如@Nico Haase 在他的回答中提到的,执行后:

$ composer require google/cloud-text-to-speech

而不是:

$ composer require google/cloud-speech

Composer 自动将以下行添加到 composer.json

{
    "require": {
        "google/cloud-text-to-speech": "^1.0"     
    }
}

然后客户端实例化没有任何问题

$client = new TextToSpeechClient();