Search code examples
javascriptphpjqueryfile-uploadwistia

jQuery File Upload with Wistia API


I've got an demo page with jQuery File Upload that is currently allowing upload of video files to the web hosting through PHP.

Code:

<?
// A list of permitted file extensions
$allowed = array('mov', 'mp4', 'avi');

if(isset($_FILES['upl']) && $_FILES['upl']['error'] == 0){

    $extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);

    if(!in_array(strtolower($extension), $allowed)){
        echo '{"status":"error"}';
        exit;
    }

    if(move_uploaded_file($_FILES['upl']['tmp_name'], 'uploads/'.$_FILES['upl']['name'])){
        echo '{"status":"success"}';
        exit;
    }
}

echo '{"status":"error"}';
exit;
?>

I need this demo to be fully working to upload video files to my Wistia gallery through their API instead of upload directory.

Working snippet for upload.php to Wistia API with video url:

<?
$data = array(
    'api_password' => '[password]',
    'project_id' => '[project_id]',
    'url' => '[video_url]'
);

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

// Send the request
$wistia_request = curl_exec($wistia);
?>

However changing these values and using it in my form doesn't work:

$data = array(
    'api_password' => '[password]',
    'project_id' => '[project_id]',
    'file' => '@' . $_FILES['upl']['name']
);

As you can see I need guidance and help. Any hints are much appreciated.

Here's some docs for this project:

http://wistia.com/doc/upload-api

https://github.com/blueimp/jQuery-File-Upload


Solution

  • Solved!

    $data = [
       'file' => "@{$_FILES['upl']['tmp_name']};filename={$_FILES['upl']['name']};type={$_FILES['upl']['type']}"
    ]