将CURL数据发送到IBM Watson进行识别

eit6fx6z  于 2022-11-13  发布在  其他
关注(0)|答案(2)|浏览(200)

我正在尝试将一个音频文件发送到IBM沃森进行识别,IBM Watson通常用于语音到文本的转换。我已经学习了HTTP Rest接口的教程,在那里我发现了这一点:

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
此命令用于识别您发送给沃森的音频文件。
下面是我使用cURL的PHP代码。

<?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);

               ?>

当我在浏览器中运行此程序时,我不断收到此错误:

{ "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 - " }

预期输出应为:

{
        "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
       }

我不知道该怎么做才能纠正错误。二进制数据字段是否正确?下面的字段:

$post = array(
                 "file" => "@" .realpath("{path}audio-file.flac")
          );
          curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

或者有其他问题...
[Note:]
我已经通过提供有效的用户名和密码成功地纠正了身份验证的问题。但是现在问题似乎不同了。下面是我代码中的一些修改:

$post = array(
                  "file" => 
           curl_file_create('<tmp_path>','file_type','file_name')
                   );

       $headers[] = "Content-Type: audio/mp3";

这些修改是因为我的音频文件是mp3扩展的。但现在在浏览器上运行脚本时,我得到:
{“代码_描述”:“错误请求”,“代码”:400,【错误】:“流为0字节,但至少需要100字节。”}
我已经检查了关于这个错误的相关帖子:400问题,但问题仍然存在。这是链接Send file via cURL from form POST in PHP
即使按照上面的链接中的答案,我的问题不去。
但当在终端中运行以下作为:
curl -X POST -u {某个用户名}:{某个密码} --header“内容类型:音频/mp3”--数据二进制@/var/www/test/96_-_陈词滥调. mp3“https://stream.watsonplatform.net/speech-to-text/api/v1/recognize
它完全按照预期的方式获取输出。但是当在浏览器上运行php脚本时,我遇到了这个问题。可能是什么地方出了问题?请给出建议。谢谢。

kg7wmglp

kg7wmglp1#

我已经解决了这个问题!!这是下面的部分,这是负责的问题...

$post = array(
              "file" => 
       curl_file_create('<tmp_path>','file_type','file_name')
               );

我必须添加一些代码到我的php文件,这是...

$data = file_get_contents(<temp_file_path>);

临时文件路径来自..

$tmpfile = $_FILES['audio']['tmp_name'];(When you are using form to upload the audio and send to Watson server)

还加了几句......

curl_setopt($ch,CURLOPT_HTTPHEADER, ['Content-Type: audio/mp3']);
curl_setopt($ch,CURLOPT_BINARYTRANSFER,TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

然后在浏览器中执行代码,结果如预期的那样完美,如下所示:

{
      "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
       }

一切都很好:D!

ffvjumwh

ffvjumwh2#

我知道这是一个老职位。我只是想分享代码,为我工作(18/08/2022)。
它工作正常,只是复制粘贴和点击刷新您的浏览器。

$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);

相关问题