0

I am calling cURL call like below but wanted to convert in to PHP format

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 '.'

Above cURL wanted to convery in to PHP format like below

    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);

but getting error of authorization but when I pass parameters to cURL like Token & audioUrl it works. but it does not works when pass parameters like above php CURL_OPT

Kevin Yan
  • 1,236
  • 11
  • 19
  • What's the value of `$header`? – Hang Apr 13 '16 at 02:56
  • header value is $header = "Authorization: Bearer ${token}"; – Harshal Borse Apr 13 '16 at 03:08
  • Have you checked if `$token` in PHP has the same value in shell? – Hang Apr 13 '16 at 03:12
  • Yes Token value is right. also I am passing parameters $params = array( "media" => $mediaURL, "action" => "media-post-response", "format" => "txt" ); – Harshal Borse Apr 13 '16 at 03:15
  • Do you know if the server will return authorization error if any of the parameters is invalid? I think you used wrong way to post a file, but usually it happens after the authentication part, check http://stackoverflow.com/questions/15200632/how-to-upload-file-using-curl-with-php – Hang Apr 13 '16 at 03:23

1 Answers1

0

Checkout the php curl official manual , it says:

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

So I think you need change your $header to $header = array("Authorization: Bearer ${token}")

Kevin Yan
  • 1,236
  • 11
  • 19