Problem with API response of Speech-To-Text of Google-Cloud, PHP programming

时间:2019-04-05 15:52:09

标签: google-cloud-platform google-cloud-speech

require __DIR__ . '/vendor/autoload.php';
use Google\Cloud\Speech\V1\SpeechClient;
use Google\Cloud\Speech\V1\RecognitionAudio;
use Google\Cloud\Speech\V1\RecognitionConfig;
use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;

$encoding = AudioEncoding::FLAC;
$sampleRateHertz = 44100;
$languageCode = 'es-CL';

$audioFile = __DIR__ . '/radio/test.flac';

$audio = (new RecognitionAudio())
    ->setUri($audioFile);

$config = (new RecognitionConfig())
    ->setEncoding($encoding)
    ->setSampleRateHertz($sampleRateHertz)
    ->setLanguageCode($languageCode);
putenv('GOOGLE_APPLICATION_CREDENTIALS=/var/www/html/key.json');

$client = new SpeechClient();

$operation = $client->longRunningRecognize($config, $audio);
$operation->pollUntilComplete();

if ($operation->operationSucceeded()) {
    $response = $operation->getResult();

    foreach ($response->getResults() as $result) {
        $alternatives = $result->getAlternatives();
        $mostLikely = $alternatives[0];
        $transcript = $mostLikely->getTranscript();
        $confidence = $mostLikely->getConfidence();
        printf('Transcript: %s' . PHP_EOL, $transcript);
        printf('Confidence: %s' . PHP_EOL, $confidence);
    }
} else {
    print_r($operation->getError());
}

$client->close();

?>
Fatal error: Uncaught Google\ApiCore\ApiException: { "message": "Request 
    contains an invalid argument.", "code": 3, "status": "INVALID_ARGUMENT", 
    "details": [] } thrown in 
    /var/www/html/vendor/google/gax/src/ApiException.php on line 139

That error is incomplete and I don't how to search that on goodle cloud platform support, please if you can help me I appreciated

1 个答案:

答案 0 :(得分:0)

对我来说最重要的第一件事是您试图通过URI传递磁盘上文件的位置。尝试以下方法,让我知道它是否适合您:

$audioFile = __DIR__ . '/radio/test.flac';

$audio = (new RecognitionAudio())
    ->setContent(file_get_contents($audioFile));