I'm trying to send an audio file for recognition to IBM Watson which is popularly used for speech to text conversion. I have followed the tutorial for the HTTP Rest interface where I discovered this :
curl -X POST -u {username}:{password}
--header "Content-Type: audio/flac"
--data-binary @{path}audio-file.flac
"https://stream.watsonplatform.net/speech-to-text/api/v1/recognize"
This command is used for recognition of the audio file you are sending to watson.
And below here is my PHP code using cURL.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
"https://stream.watsonplatform.net/speech-to-
text/api/v1/recognize");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$post = array(
"file" => "@" .realpath("{path}audio-file.flac")
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, "{username}" . ":" .
"{password}");
$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);
}
else{
print_r($result);
}
curl_close ($ch);
?>
When I run this in browser, I keep getting this error:
{ "code" : 401 , "error" : "Not Authorized" , "description" : "2018-05-03T03:15:09-05:00, Error ERCDPLTFRM-INVLDCHR occurred when accessing https://stream.watsonplatform.net/speech-to-text/api/v1/recognize, Tran-Id: stream01-896101253 - " }
The expected output should be:
{
"results": [
{
"alternatives": [
{
"confidence": 0.891,
"transcript": "several tornadoes touch down as a line
of severe thunderstorms swept through Colorado on
Sunday "
}
],
"final": true
}
],
"result_index": 0
}
I don't understand what to do in order to rectify the mistake. Is the binary data field correct? The one below:
$post = array(
"file" => "@" .realpath("{path}audio-file.flac")
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
or there is some other problem...
[Note:]
I have successfully rectified the problem of authentication by providing a valid username and password. But now the problem seems to be different. Some of the modifications in my code below:
$post = array(
"file" =>
curl_file_create('<tmp_path>','file_type','file_name')
);
$headers[] = "Content-Type: audio/mp3";
These modifications are made as my audio file is mp3 extended. But now while running the script on browser, I get:
{ "code_description": "Bad Request", "code": 400, "error": "Stream was 0 bytes but needs to be at least 100 bytes." }
I have checked a relevant post regarding this error:400 problem but still the problem persists. This was the link Send file via cURL from form POST in PHP
Even after following the answers in the above link, my problem does not go.
But when the run the following in terminal as :
curl -X POST -u {some_username}:{some_password} --header "Content-Type: audio/mp3" --data-binary @/var/www/test/96_-_Cliches.mp3 "https://stream.watsonplatform.net/speech-to-text/api/v1/recognize"
It is perfectly fetching the output as expected. But when running the php script on browser, I get this problem. What could have possibly went wrong? Please suggest what to do. Thank you.