2

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 Answer 1

0

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.