【问题标题】:PHP - Google Speech API: Class 'Google\Cloud\Speech\SpeechClient' not foundPHP - Google Speech API:找不到类“Google\Cloud\Speech\SpeechClient”
【发布时间】:2017-11-01 09:51:28
【问题描述】:

我希望有人可以帮助我弄清楚如何让 google 的语音转文本 api 运行。

https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/speech/api

我要运行的具体代码是:src/functions/transcribe_sync.php

在末尾添加一行来调用函数:

transcribe_sync("mono.flac", 'en-US');

它找不到依赖项。我一直在和作曲家一起玩,但无法弄清楚。

这是错误:

php transcribe_sync.php
PHP Fatal error:  Uncaught Error: Class 'Google\Cloud\Speech\SpeechClient' not found in /home/bhaag/Documents/php-docs-samples-master/speech/api/src/functions/transcribe_sync.php:45
Stack trace:
#0 /home/bhaag/Documents/php-docs-samples-master/speech/api/src/functions/transcribe_sync.php(55): Google\Cloud\Samples\Speech\transcribe_sync('mono.flac', 'en-US')
#1 {main}
  thrown in /home/bhaag/Documents/php-docs-samples-master/speech/api/src/functions/transcribe_sync.php on line 45

这是我的 composer.json 文件:

{
    "require": {
        "google/cloud-speech": "^0.2",
        "google/cloud-storage": "^1.0",
        "google/gax": "^0.8.1",
        "google/proto-client-php": "^0.10.0",
        "symfony/console": "^3.0",
        "php-ffmpeg/php-ffmpeg": "^0.9.3"
    },
    "autoload": {
        "psr-4": {
            "Google\\Cloud\\Samples\\Speech\\": "src/"
        },
        "files": [
            "src/functions/streaming_recognize.php",
            "src/functions/transcribe_async.php",
            "src/functions/transcribe_async_gcs.php",
            "src/functions/transcribe_sync.php",
            "src/functions/transcribe_sync_gcs.php"
        ]
    },
    "require-dev": {
        "phpunit/phpunit": "~4"
    }
}

我已经能够运行完整的语音示例,因此我知道我已正确设置了所有凭据,但使用完整示例(主目录中的speech.php)似乎真的很神秘。

我是不是走错了路?感谢您的帮助!

【问题讨论】:

    标签: php composer-php google-cloud-platform


    【解决方案1】:

    样本的构建方式是通过根目录中的speech.php 文件运行。要直接运行transcribe_sync.php,您需要包含composer autoloader:

    require_once "/path/to/vendor/autoload.php";
    

    /path/to 替换为您的作曲家依赖项的安装路径。

    或者,(我推荐这个选项),在不更改示例代码的情况下运行,在命令行运行:

    php speech.php transcribe mono.flac
    

    【讨论】:

    • 哦,谢谢!这确实解决了依赖问题,但它仍然给我一个错误:PHP Parse error: syntax error, unexpected 'namespace' (T_NAMESPACE) in /home/bhaag/Documents/php-docs-samples-master/speech/api/transcribe_sync。 php 在第 28 行 感谢您的帮助!
    • 我在
    • 请务必在该行的末尾添加一个分号。我已经更新了我的答案以表明这一点。
    • 哇。我不敢相信我错过了一个分号。还不习惯 php 的错误。感谢您指出并感谢您解决了我的问题