Reputation: 21
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.
Upvotes: 1
Views: 1018
Reputation: 3531
I know this is an old post. I Just want to share code that works for me (18/08/2022).
It works fine, just copy-paste and hit refresh your browser.
$fname = 'YourFilePath'; //Example: 'C:/audios/audio-file.flac;
$cfile = new CURLFile(realpath($fname));
$file = array ('file' => $cfile);
$urlx = 'Paste here your url'; //It is something like this: https://api.us-south.speech-to-text.watson.cloud.ibm.com/instances/xxxxxxxxxxx
$curlx = curl_init();
curl_setopt($curlx, CURLOPT_URL, $urlx);
curl_setopt($curlx, CURLOPT_USERPWD, 'apikey:PASTE_EXACTLY_HERE_YOUR_API_KEY');
curl_setopt($curlx, CURLOPT_HTTPHEADER, ['Content-Type: audio/flac']);
curl_setopt($curlx, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curlx, CURLOPT_POST, TRUE);
curl_setopt($curlx, CURLOPT_POSTFIELDS, $file);
curl_setopt($curlx, CURLOPT_MAX_SEND_SPEED_LARGE, 40000);
curl_setopt($curlx, CURLOPT_BINARYTRANSFER, TRUE);
$datax = curl_exec($curlx);
curl_close($curlx);
print($datax);
Upvotes: 0
Reputation: 21
I have solved the issue!! This was the part below which was responsible for the problem...
$post = array(
"file" =>
curl_file_create('<tmp_path>','file_type','file_name')
);
I have to add some codes to my php file which were...
$data = file_get_contents(<temp_file_path>);
The tmp_file_path comes from ..
$tmpfile = $_FILES['audio']['tmp_name'];(When you are using form to upload the audio and send to Watson server)
Some other lines were also added...
curl_setopt($ch,CURLOPT_HTTPHEADER, ['Content-Type: audio/mp3']);
curl_setopt($ch,CURLOPT_BINARYTRANSFER,TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
Then are executing the code in browser, the result was perfect as expected like this below:
{
"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
}
Well all taken care of :D!
Upvotes: 1