php cuRL 响应 "unable to transcode data stream audio/flac -> audio/x-float-array" - IBM Watson 语音转文本 API
php cuRL response "unable to transcode data stream audio/flac -> audio/x-float-array" - IBM Watson Speech to text API
我不太了解如何使用 cURL.I 我正在尝试使用 IBM Watson API 将语音转换为文本。当我尝试不使用参数进行转换时(翻译英文
音频文件),我得到了没有任何错误的响应。
但是当我添加
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'model'=>'ja-JP_NarrowbandModel'
))
它returns
{ "code_description": "Bad Request", "code": 400, "error": "unable to
transcode data stream audio/flac -> audio/x-float-array " }
我不确定是我的语法有问题还是其他地方出了问题。
我阅读了以下文档:https://console.bluemix.net/docs/services/speech-to-text/http.html#http
<?php
$ch = curl_init();
$file = file_get_contents('audio-file.flac');
curl_setopt($ch, CURLOPT_URL, 'https://stream.watsonplatform.net/speech-to-text/api/v1/recognize');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $file);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'apikey' . ':' . 'MY_API_HERE');
$headers = array();
$headers[] = 'Content-Type: audio/flac';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'model'=>'ja-JP_NarrowbandModel'
));
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
print_r($result);
您正在设置 CURLOPT_POSTFIELDS
两次,一次使用您的文件内容,第二次使用包含 'model'=>'ja-JP_NarrowbandModel'
.
的数组
根据the documentation,您可以将模型作为查询参数传递。
尝试这样的事情(未测试):
<?php
$file = file_get_contents('audio-file.flac');
$url = 'https://stream.watsonplatform.net/speech-to-text/api/v1/recognize';
$model = 'ja-JP_NarrowbandModel';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . '?model=' . $model);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $file);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'apikey' . ':' . 'MY_API_HERE');
$headers = array();
$headers[] = 'Content-Type: audio/flac';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
print_r($result);
我不太了解如何使用 cURL.I 我正在尝试使用 IBM Watson API 将语音转换为文本。当我尝试不使用参数进行转换时(翻译英文 音频文件),我得到了没有任何错误的响应。
但是当我添加
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'model'=>'ja-JP_NarrowbandModel'
))
它returns
{ "code_description": "Bad Request", "code": 400, "error": "unable to
transcode data stream audio/flac -> audio/x-float-array " }
我不确定是我的语法有问题还是其他地方出了问题。
我阅读了以下文档:https://console.bluemix.net/docs/services/speech-to-text/http.html#http
<?php
$ch = curl_init();
$file = file_get_contents('audio-file.flac');
curl_setopt($ch, CURLOPT_URL, 'https://stream.watsonplatform.net/speech-to-text/api/v1/recognize');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $file);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'apikey' . ':' . 'MY_API_HERE');
$headers = array();
$headers[] = 'Content-Type: audio/flac';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'model'=>'ja-JP_NarrowbandModel'
));
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
print_r($result);
您正在设置 CURLOPT_POSTFIELDS
两次,一次使用您的文件内容,第二次使用包含 'model'=>'ja-JP_NarrowbandModel'
.
根据the documentation,您可以将模型作为查询参数传递。
尝试这样的事情(未测试):
<?php
$file = file_get_contents('audio-file.flac');
$url = 'https://stream.watsonplatform.net/speech-to-text/api/v1/recognize';
$model = 'ja-JP_NarrowbandModel';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . '?model=' . $model);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $file);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'apikey' . ':' . 'MY_API_HERE');
$headers = array();
$headers[] = 'Content-Type: audio/flac';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
print_r($result);