Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将CURL数据发送到IBM Watson进行识别_Curl_Ibm Watson - Fatal编程技术网

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

将CURL数据发送到IBM Watson进行识别,curl,ibm-watson,Curl,Ibm Watson,我正在尝试向IBM Watson发送一个音频文件以进行识别,IBM Watson通常用于语音到文本的转换。我遵循了HTTP Rest接口的教程,在那里我发现了以下内容: curl -X POST -u {username}:{password} --header "Content-Type: audio/flac" --data-binary @{path}audio-file.flac “” 此命令用于识别发送给watson的音频文件 下面是我使用cURL的PHP代码 <

我正在尝试向IBM Watson发送一个音频文件以进行识别,IBM Watson通常用于语音到文本的转换。我遵循了HTTP Rest接口的教程,在那里我发现了以下内容:

curl -X POST -u {username}:{password}
--header "Content-Type: audio/flac"
--data-binary @{path}audio-file.flac
“”

此命令用于识别发送给watson的音频文件

下面是我使用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);

               ?>
预期产出应为:

{
        "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);
或者还有其他问题

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

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

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

       $headers[] = "Content-Type: audio/mp3";
$post=array(
“文件”=>
curl\u file\u create(“”、'file\u type'、'file\u name')
);
$headers[]=“内容类型:音频/mp3”;
这些修改是在我的音频文件被mp3扩展时进行的。但现在在浏览器上运行脚本时,我得到:

{“code_description”:“错误请求”,“code”:400,“错误”:“流为0字节,但至少需要100字节。”}

我已经查看了一篇关于这个错误的相关帖子:400问题,但问题仍然存在。这就是链接

即使按照上面链接中的答案,我的问题仍然没有解决

但是,当在终端中运行以下命令时:

curl-X POST-u{some_username}:{some_password}--header“Content Type:audio/mp3”--data binary@/var/www/test/96_u-Cliches.mp3“”


它完美地获取了预期的输出。但在浏览器上运行php脚本时,我遇到了这个问题。可能出了什么问题?请建议怎么做。谢谢。

我已经解决了这个问题!!这是下面导致问题的部分

$post = array(
              "file" => 
       curl_file_create('<tmp_path>','file_type','file_name')
               );
还添加了一些其他行

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

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
       }