2

I am hoping to build a video upload using the Streamable API and PHP with cURL. https://streamable.com/documentation#upload-video-file

What I'm trying to accomplish is:

  1. User fills out a form of info and selects a video file from their computer/device to upload
  2. Submits the form, PHP handles it from there to talk to the Streamable API to upload the video via the form to my Streamable account, then return the shortcode from Streamable for me to store in a MySQL database with the rest of their info

I've tried, with success, using the curl command via terminal. But, I'm having issues with pulling it off via php form submission.

This is an example of the command I used in terminal to upload, which worked:

curl https://api.streamable.com/upload -u my_email:my_pass -F file=@path/to/file.mp4

With PHP, I have a pretty simple cURL script thanks to the tons of help online. I guess you could say I'm pretty new to using cURL.

$url = 'https://api.streamable.com/upload -u my_email.com:my_pass -F file=@path/to/file.mp4';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);

With that, I'm getting a HTTP 400 error. https://streamable.com/documentation#errors

..codes in the 400 range indicate client errors...

I guess that's what's messing me up here?

I tried it this way, but I get the same error.

$pass = 'my_email:my_pass';
$postFields = array(
 'file' => '/path/to/file.mp4',
 'title' => 'Example Title'
);
$url = 'https://api.streamable.com/upload';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, $pass);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);

I used print_r(curl_getinfo($ch)); to see what's happening, and this is what it's spitting out - maybe this can be useful for some help:

Array ( [url] => https://api.streamable.com/upload -u my_email:my_pass -F file=@/Path/to/file.mp4 [content_type] => [http_code] => 400 [header_size] => 66 [request_size] => 277 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.36033 [namelookup_time] => 0.00138 [connect_time] => 0.082871 [pretransfer_time] => 0.283219 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => 0 [upload_content_length] => 275 [starttransfer_time] => 0.36031 [redirect_time] => 0 [redirect_url] => [primary_ip] => xx.xx.xxx.xxx [certinfo] => Array ( ) [primary_port] => 443 [local_ip] => 192.168.0.1 [local_port] => 57288 )

Dharman
  • 30,962
  • 25
  • 85
  • 135
aw1986
  • 73
  • 5

2 Answers2

2

There are couple of things you need to change here:

  • Set CURLOPT_SSL_VERIFYPEER to false. It can be used to verify peer's certificate. If we specify it as false, it will accept any server(peer) certificate.

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    

    Here's a good read on the implication of turning CURLOPT_SSL_VERIFYPEER on and off, If CURLOPT_SSL_VERIFYPEER is false, is the data transfer no longer secure?

  • CURLOPT_POSTFIELDS is used to specify full data we want to submit with the POST request. The $postFields array should be converted to URL-encoded query string using http_build_query() function, so that it could be sent as application/x-www-form-urlencoded.

    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postFields));
    
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • Is it worth adding a note here about the security implications of turning off certificate verification? It may not be obvious to everyone. – halfer Jul 05 '17 at 17:39
  • 2
    @halfer Ah, good thought, I should add that as well. Give me a moment. Edit: Updated answer. – Rajdeep Paul Jul 05 '17 at 17:45
  • Thanks for the insight! I have made the changes cited above (taking note of the secure issue), but it gave me the same result. I stopped using the POSTFIELDS option, and just went with the straight full line `$url` option. – aw1986 Jul 06 '17 at 12:29
  • `$url = str_replace(' ','%20','https://api.streamable.com/upload -u my_email:my_pass -F file=@/Path/to/file.mp4'); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $server_output = curl_exec ($ch); print_r(curl_getinfo($ch)); print_r(curl_error($ch)); print_r($server_output); //this actually displays the Streamable 404 on return page? curl_close ($ch);` Now, a 404 error using the `str_replace` as I read online about URL whitespace. – aw1986 Jul 06 '17 at 12:31
  • Screenshot of error with the streamable 404 being printed by the output -> [link](http://imgur.com/a/vyw8P) – aw1986 Jul 06 '17 at 12:36
0

Try this:

$fields = array("file"=>curl_file_create("video.mp4"));

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.streamable.com/upload");
curl_setopt($ch, CURLOPT_USERPWD, "email:pass");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields );

$result = curl_exec($ch);

I think curl_file_create makes the trick, it seems using @fileName no more work for PHP 5.5+

DeepBlue
  • 684
  • 7
  • 23