我试图将命令行卷曲转换为php curl。这是IBM Speech to Text服务。线路如下:
curl -X POST -u "{username}":"{password}"
--header "Content-Type: audio/flac"
--data-binary @audio-file.flac
"https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?timestamps=true&word_alternatives_threshold=0.9"
我试过这个:
$url = "https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?timestamps=true&word_alternatives_threshold=0.9";
$postData = array(
'upload' => '@D:\Others\xampp2\htdocs\test\test.wav',
'Accept' => "application/json",
);
foreach($postData as $key=>$value){
$fields_string .= $key.'='.$value.'&';
}
$fields_string = rtrim($fields_string, '&');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, "MYUSER:MYPASS");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: audio/wav',
'continuous: true',
));
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch,CURLOPT_TIMEOUT,1000);
$result = curl_exec($ch);
echo curl_error($ch);
curl_close($ch);
var_dump($result);
但是我收到了这条消息:
string(129) "{
"code_description": "Bad Request",
"code": 400,
"error": "Stream was 69 bytes but needs to be at least 100 bytes."
}"
可能是什么问题?任何建议都会非常有用。
更新
$postData = array(
'upload' => '@D:\Others\xampp2\htdocs\test\test.wav',
'Accept' => "application/json",
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, "MYUSER:MYPASS");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: audio/wav',
'continuous: true',
));
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch,CURLOPT_TIMEOUT,1000);
$result = curl_exec($ch);
echo curl_error($ch);
curl_close($ch);
var_dump($result);
现在收到此错误:
string(139) "{
"code_description": "Bad Request",
"code": 400,
"error": "unable to transcode data stream audio/wav -> audio/x-float-array "
}"
答案 0 :(得分:0)
您正在构建自己的POST字符串:
$postData = array(
'upload' => '@D:\Others\xampp2\htdocs\test\test.wav',
'Accept' => "application/json",
);
foreach($postData as $key=>$value){
$fields_string .= $key.'='.$value.'&';
}
如果您提供字符串,CURL会按原样发送该字符串,而不进行进一步处理。这意味着CURL不会解析提供的$fields_string
以查看是否有任何@
- 前缀值指示文件上传,并且您最终将文字文本@D:\etc...
发送到服务器,而不是你认为应该去的文件的内容。
不要构建自己的查询字符串/正文 - 只需将原始数组提供给CURL。它会做其余的事情:
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
^^^^^^^---your original array