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 u盘问题:盘符不显示 无媒体
  • ¥50 R语言读取nc按月均值转为tif
  • ¥30 智能车串级pid调参
  • ¥15 visual studio code翻译老是错误
  • ¥20 卫星测高数据的高程转换
  • ¥15 爬取招聘网站数据信息
  • ¥15 安装完tensorflow,import tensorflow as tf后报错,如何解决?
  • ¥15 ultralytics库导出onnx模型,模型失去预测能力
  • ¥15 linux下点对点协议连接2个USB串口的硬件流量控制问题
  • ¥15 SQL数据自动生成问题