我想通過使用PHP調用Google Cloud Speech API並出現問題。Google Cloud Speech API使用php
$stturl = "https://speech.googleapis.com/v1beta1/speech:syncrecognize?key=xxxxxxxxxxxx";
$upload = file_get_contents("1.wav");
$upload = base64_encode($upload);
$data = array(
"config" => array(
"encoding" => "LINEAR16",
"sampleRate" => 16000,
"languageCode" => "en-US"
),
"audio" => array(
"Content" => $upload,
)
);
$jsonData = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $stturl);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
$result = curl_exec($ch);
結果表明它是無效的JSON PAYLOAD。
{ 「錯誤」: 「無效的JSON有效載荷收到 未知名稱\」 內容\ 「在 '聲音'::找不到域」{ 「代碼」: 「消息」 400, 「狀態」 : 「INVALID_ARGUMENT」,「details」:[{「@type」: 「type.googleapis.com/google.rpc.BadRequest」,「fieldViolations」:[{「{」「field」,「audio」,「description」 :「收到無效的JSON有效負載 'audio'處的未知名稱\」content \「:無法找到字段。」 }]}]}}「
我想這是因爲$上傳配置不正確。 據谷歌雲語音API,它應該是 」base64編碼字符串「。 https://cloud.google.com/speech/reference/rest/v1beta1/RecognitionAudio
這就是爲什麼我用BASE64_ENCODE功能,但它似乎JSON沒有正確地處理這個值。 有什麼想法?
你應該''base64_encode($上傳)'並且將'Content'改爲'content'。應該工作正常。 – BadHorsie