为什么在 App Engine 上尝试创建 Google 类 TextToSpeechClient

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


“注意:PHP 消息:PHP 致命错误:未捕获错误:在 /app/web/get_voices2.php:46 中找不到类 'Google\Cloud\TextToSpeech\V1\TextToSpeechClient'”


我的 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 应用程序的 Web 目录结构位于同一目录中


我的 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

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


温温酱
浏览 127回答 2
2回答

婷婷同学_

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

函数式编程

我只是用一个有效的解决方案来跟进我的问题。$ composer require google/cloud-text-to-speech代替:$ composer require google/cloud-speechComposer 自动将以下行添加到 composer.json{     "require": {         "google/cloud-text-to-speech": "^1.0"          } }然后客户端被实例化没有任何问题$client = new TextToSpeechClient();
打开App,查看更多内容
随时随地看视频慕课网APP