将中的CURL转换为PHP CURL调用,但出现授权错误


Convert CURL in to PHP CURL call but getting error of authorization

我正在调用下面这样的cURL调用,但希望转换为PHP格式

curl https://apis.voicebase.com/v2-beta/media '
--header "Authorization: Bearer $token" '
--form media=@/home/harshal/Desktop/100Hz_44100Hz_16bit_30sec.wav'
| tee media-post-response.json | jq '.'

上面的cURL希望转换为PHP格式,如下所示

    set_time_limit(0);
    $ch = curl_init();      
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 0);
    curl_setopt($ch, CURLOPT_LOW_SPEED_LIMIT, 0);
    curl_setopt($ch, CURLOPT_LOW_SPEED_TIME, 60);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
    curl_setopt($ch, CURLOPT_URL, $url);                
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);      
    $result = json_decode(curl_exec($ch),TRUE);
    curl_close($ch);

但收到授权错误,但当我将参数传递到cURL时,如Token&audioUrl有效。但当传递参数(如上面的php CURL_OPT

)时,它不起作用

查看php curl官方手册,上面写着:

CURLOPT_HTTPHEADER  
    An array of HTTP header fields to set, in the format array('Content-type:   text/plain', 'Content-length: 100')

所以我认为您需要将$header更改为$header = array("Authorization: Bearer ${token}")