【问题标题】:Why do I get a server fatal error when trying to create an instance of Google's class TextToSpeechClient on App Engine?尝试在 App Engine 上创建 Google 类 TextToSpeechClient 的实例时,为什么会出现服务器致命错误?
【发布时间】:2020-05-12 17:51:23
【问题描述】:

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

"注意:PHP 消息: PHP致命错误:未捕获的错误:类 'Google\Cloud\TextToSpeech\V1\TextToSpeechClient' 未在 /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

我希望我提供了完整的信息。

【问题讨论】:

  • 这与 Composer 本身或“app.yaml”有什么关系?您尝试过什么调试问题?

标签: php google-app-engine composer-php google-text-to-speech app.yaml


【解决方案1】:

根据它的repository,该类在包google/cloud-text-to-speech 中给出 - 但根据您的composer.json,您不需要该包。

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

【讨论】:

    【解决方案2】:

    我只是用一个有效的解决方案来跟进我的问题。正如@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();
    

    【讨论】:

    • 很好地找到了解决方案!你能accept your own answer吗?它将使其更加明显,并在您找到解决方案时帮助遇到相同问题的人。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多