Google Speech Api PHP失败了

时间:2016-09-23 07:38:27

标签: php google-speech-api

我正在尝试在PHP中使用Google语音API来从音频中读取文本,但它失败并出现以下情况:请求必须包含“config”字段。

这是代码:

<?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文档,所以任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

尝试:

$audio->setContent(base64_encode(file_get_contents('c1.wav')));