2

我正在尝试在 PHP 中使用 Google 语音 API 从音频中读取文本,但它失败并显示以下内容:请求必须包含“配置”字段。

这是代码:

<?php

require '../lib/vendor/autoload.php';

$client = new Google_Client();
$client->useApplicationDefaultCredentials();

$client->setAuthConfig(__DIR__.'/config.json');

$client->authorize();

$guzzleClient = new \GuzzleHttp\Client(array( 'curl' => array( CURLOPT_SSL_VERIFYPEER => false)));
$client->setHttpClient($guzzleClient);
$client->addScope('https://www.googleapis.com/auth/cloud-platform');

$speechService = new Google_Service_Speech($client);

$syncRecognize = new Google_Service_Speech_SyncRecognizeRequest();

$config = new Google_Service_Speech_RecognitionConfig();
$config->audioChannels = 1;
$config->encoding = 'LINEAR16';//'audio/wav';
$config->sampleRate = 8000;

$audio = new Google_Service_Speech_RecognitionAudio();
$audio->setContent(file_get_contents('c1.wav'));

$syncRecognize->setAudio($audio);
$syncRecognize->setConfig($config);


$response = $speechService->speech->syncrecognize($syncRecognize);

我收到以下信息:

Fatal error: Uncaught exception 'Google_Service_Exception' with message '{
  "error": {
    "code": 400,
    "message": "Invalid recognition 'config': request must contain 'config' field.",
    "errors": [
      {
        "message": "Invalid recognition 'config': request must contain 'config' field.",
        "domain": "global",
        "reason": "badRequest"
      }
    ],
    "status": "INVALID_ARGUMENT"
  }
}

我找不到任何 PHP 文档,因此将不胜感激。

4

1 回答 1

0

尝试:

$audio->setContent(base64_encode(file_get_contents('c1.wav')));
于 2017-01-13T10:58:32.223 回答