PHP的卷曲响应“无法进行代码转换的数据流的音频/ FLAC - >音频/ X-浮阵列” - IBM沃森语音到文本API

问题描述 投票:2回答:1

我不知道很多关于如何使用cURL.I我试图将语音转换使用IBM沃森API为文本。当我尝试将其转换,而无需使用参数(翻译英文音频文件),我得到没有任何错误的响应。

但是,当我添加

curl_setopt($ch, CURLOPT_POSTFIELDS, array(
      'model'=>'ja-JP_NarrowbandModel'
))

它返回

{ "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);
php curl ibm-watson
1个回答
0
投票

要设置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);
© www.soinside.com 2019 - 2024. All rights reserved.