PHPを使用してGoogle Cloud Speech APIを呼び出そうとしていますが、問題が発生しました。Googleクラウドスピーチ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と表示されます。
{ "エラー":{ "コード":400、 "メッセージ": "無効なJSONペイロードを受信 不明な名前\。" コンテンツ\ " 'オーディオ' で:フィールドを見つけることができません" と、 "状態" : "field": "audio"、 "description": "INVALID_ARGUMENT"、 "details":[{"@type": "type.googleapis.com/google.rpc.BadRequest"、 "fieldViolations" : "無効なJSONペイロードが受信されました 'audio'の名前が不明です:フィールドが見つかりません。 }]}]}}」
私は$のアップロードが正しく設定されていないため、これがあると思います。 GoogleクラウドスピーチAPIをよれば、あるべき "base64でエンコードされた文字列"。 https://cloud.google.com/speech/reference/rest/v1beta1/RecognitionAudio
私はBASE64_ENCODE機能を使用する理由ですが、JSONがこの値を正しく処理しませんようです。 任意の考え?
'base64_encode($ upload)'と 'Content'を' content'に変更する必要があります。うまく動作するはずです。 – BadHorsie