dongqindan4406 2019-02-03 10:12
浏览 125
已采纳

php cuRL响应“无法转码数据流音频/ flac - > audio / x-float-array” - IBM Watson Speech to text API

I don't know much about how to use cURL.I am trying to convert Speech to Text using IBM Watson API. When I try to convert it without using parameters(Translate English Audio File), I get a response without any error.

But when I add

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

It returns

{ "code_description": "Bad Request", "code": 400, "error": "unable to 
transcode data stream audio/flac -> audio/x-float-array " }

I am not sure if there is an issue in my Syntax or something else is going wrong there.

I read docs from : 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);
  • 写回答

1条回答 默认 最新

  • dongqie2010 2019-02-03 12:45
    关注

    You are setting CURLOPT_POSTFIELDS twice, once with the content of your file and a second time with an array containing 'model'=>'ja-JP_NarrowbandModel'.

    According to the documentation, you can pass the model as a query parameter.

    Try something like this (not tested):

    <?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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)