我试图通过使用PHP调用Google Cloud Speech API并遇到问题 .
$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);
结果表明它是INVALID JSON PAYLOAD .
{“error”:{“code”:400,“message”:“收到的JSON有效负载无效 . 未知名称\”内容\“at'audic':找不到字段 . ”,“status”:“INVALID_ARGUMENT”,“详细信息“:[{”@ type“:”type.googleapis.com/google.rpc.BadRequest“,”fieldViolations“:[{”field“:”audio“,”description“:”收到无效的JSON有效负载 . 未知名称“音频”中的“内容”:找不到字段 . “ }]}]}}“
我认为这是因为$ upload未正确配置 . 根据Google Cloud Speech API,它应该是"A base64-encoded string" . https://cloud.google.com/speech/reference/rest/v1beta1/RecognitionAudio
这就是我使用 base64_encode 函数的原因,但似乎JSON没有正确处理这个值 . 有什么想法吗?
2 回答
您需要将格式正确的输入构造为数组,然后对其进行json编码 . 例如,要发送文件,base64将其编码为“内容”并提交给API,如下所示:
请制作'内容'而不是'内容'
小写字母'c'
它为我工作 .