如何用php

时间:2018-05-01 04:27:17

标签: php curl

我刚开始卷曲。 我有这个卷曲代码。 但我不知道用php运行这个

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代码中?

1 个答案:

答案 0 :(得分:1)

良好的方法是使用文件句柄和CURLOPT_INFILE,这将适用于任何大小的文件,并允许在从磁盘读取整个文件之前启动上载,因此它更快并且只使用少量内存,无论文件有多大。然而,快速简单的方法,将整个文件放在内存中,并且在整个文件被读入ram之前不开始上传,因此不适合大文件,简单来说就是: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 );