curl -X POST -u "{username}":"{password}"
--header "Content-Type: audio/flac"
--data-binary "@audio-file1.flac"
"https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?timestamps=true&word_alternatives_threshold=0.9&keywords=%22colorado%22%2C%22tornado%22%2C%22tornadoes%22&keywords_threshold=0.5"
这是我的PHP代码。但不确定我是否正确。
$s = curl_init();
curl_setopt($s, CURLOPT_URL, 'https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?timestamps=true&word_alternatives_threshold=0.9&keywords=%22colorado%22%2C%22tornado%22%2C%22tornadoes%22&keywords_threshold=0.5');
curl_setopt($s, CURLOPT_POST, 1);
curl_setopt($s, CURLOPT_POSTFIELDS, http_build_query([
'--header' => "Content-Type: audio/flac",
'--data-binary' => '@audio-file1.flac'
]));
curl_exec($s);
curl_close($s);
请帮我如何将-u“{username}”:“{password}”添加到php代码中?
答案 0 :(得分:1)
curl_setopt($ch,CURLOPT_POSTFIELDS,file_get_contents($filename));
,但是...使用好方法的curl命令的粗略等价是:
$ch = curl_init ();
$filename = "audio-file1.flac";
$fileh = fopen ( $filename, 'rb' );
curl_setopt_array ( $ch, array (
CURLOPT_USERPWD => "{username}:{password}",
CURLOPT_HTTPHEADER => array (
'Content-Type: audio/flac'
),
CURLOPT_POST => 1,
CURLOPT_INFILE => $fileh,
CURLOPT_INFILESIZE => filesize ( $filename ),
CURLOPT_URL => "https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?timestamps=true&word_alternatives_threshold=0.9&keywords=%22colorado%22%2C%22tornado%22%2C%22tornadoes%22&keywords_threshold=0.5",
CURLOPT_USERAGENT => 'libcurl/' . curl_version () ['version'] . '; php/' . PHP_VERSION
) );
// curl_setopt ( $ch, CURLOPT_URL, '127.0.0.1:9999' );
curl_exec ( $ch );
fclose ( $fileh );
curl_close ( $ch );