0

Does anyone know how to upload a video to Wistia using PHP cURL from a file upload input specified within a form? This is my code but I cannot seem to get this to work with the current API. I’ve looked at a few similar posts on this subject but still no luck...

<?php
$pathToFile = $_FILES['fileName']['tmp_name']; /* /tmp/filename.mov */
$nameOfFile = $_FILES['fileName']['name']; /* filename.mov */

$data = array(
'api_password' => '********',
'file' => '@'.$pathToFile,
'project_id' => '********'
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, "https://upload.wistia.com" );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
$result = curl_exec($ch);
curl_close($ch);
?>

UPDATE - FULL CODE (still not working)

cURL returns false for both curl_exec and curl_error when using "'file' => '@'.$pathToFile" but works fine when using "'url' = 'http://example.com/file.mov'... Perhaps I'm not processing the form properly? Here's the full code:

<?php
if ($_POST['submit']) {
    $filePath = $_FILES['fileUploaded']['tmp_name'];
    $fileName = $_FILES['fileUploaded']['name'];
    $fileSize = $_FILES['fileUploaded']['size'];

    echo("File Path: ");
    echo $filePath;
    echo '<br>';

    $data = array(
        'api_password'  => '******',
        /* 'url'            => 'http://example.com/file.mov', WORKS */
        'file'          => '@'.$filePath, /* DOES NOT WORK */
        'project_id'    => '******'
    );

    // WORKING CMD LINE
    // curl -i -F api_password=****** -F file=@file.mov https://upload.wistia.com/

    /* cURL */
    $chss = curl_init('https://upload.wistia.com');
    curl_setopt_array($chss, array(
        CURLOPT_POST => TRUE,
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_POSTFIELDS => http_build_query($data)
    ));

    // Send the request
    $KReresponse = curl_exec($chss);
    $KError = curl_error($chss);

    // Decode the response
    $KReresponseData = json_decode($KReresponse, TRUE);

    echo '<br>';
    echo("Response:");
    print_r($KReresponseData);

    echo '<br>';
    echo("Error:");
    print_r($KError);
}
?>
<form name="upload-form" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
    <input type="file" name="fileUploaded">
    <input type="submit" name="submit" value="Upload">
</form>

Any help would be much appreciated!

Gismmo
  • 329
  • 4
  • 12
  • It seems correct. Can you check what curl_error($ch) returns? – Bhavya Shaktawat Nov 20 '14 at 10:17
  • Looks fine for me as well. Have you tried command-line curl? It's usually quicker for testing: curl -vX POST -F "project_id=********&api_password=********&file=@/path/to/file" https://upload.wistia.com – Javier C. H. Nov 20 '14 at 10:24
  • Please see my update above @Nick - something's still not quite right – Gismmo Nov 20 '14 at 11:37
  • Please cross check upload_max_filesize set in php.ini file. It could be possible that the file you are uploading is beyond that limit. – Bhavya Shaktawat Nov 21 '14 at 04:52
  • Yes we have double checked, and that does not seem to be a problem. we have even tried uploading a file which was 2mb. – Gismmo Nov 21 '14 at 12:21

1 Answers1

0

Try this:

$filePath = "@{$_FILES['fileUploaded']['tmp_name']};filename={$_FILES['fileUploaded']['name']};type={$_FILES['fileUploaded']['type']}";
shahin8r
  • 113
  • 2
  • 7