2016-09-23 107 views
2

我試圖在PHP中使用Google語音API從音頻中讀取文本,但失敗的原因如下:請求必須包含'config'字段。Google Speech Api PHP失敗

這是代碼:

<?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文件,所以任何幫助,將不勝感激。

+0

你弄清楚這一個? – dearsina

回答

0

嘗試:

$audio->setContent(base64_encode(file_get_contents('c1.wav'))); 
相關問題