2

I'm trying to post files to a 3rd party API from my PHP app. This works from the command line:

curl -F "file=@move_file.MOV" "https://upload.wistia.com?project_id=pbmcmua3ot&username=api&api_password=xxxxx_apikey_yyyyy"

But I can't get it to work using PHP's curl:

    $data = array(
        'username'      => $username,
        'api_password'  => $api_password,
        'file'          => fopen($tmp_filename, 'r'),
        'project_id'    => $project_hashed_id,
        );


    $ch = curl_init();
    //curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, "https://upload.wistia.com" );
    curl_setopt($ch, CURLOPT_UPLOAD, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 86400); // 1 Day Timeout

    curl_setopt($ch, CURLOPT_INFILE, fopen($tmp_filename, 'r') );
    curl_setopt($ch, CURLOPT_NOPROGRESS, false);
    curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
    curl_setopt($ch, CURLOPT_INFILESIZE, filesize($tmp_filename));

    curl_setopt($ch, CURLOPT_VERBOSE, 1); //for debugging
    curl_setopt($ch, CURLOPT_HEADER, true);

    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

    $result = curl_exec($ch);
    curl_close($ch);
    return json_decode($result);

I think CURLOPT_INFILE is the problem but I'm not sure. Thanks in advance for any help you can give me.

UPDATE1

    $data = array(
        'username'      => $username,
        'api_password'  => $api_password,
        'file'          => '@'.$tmp_filename,
        'project_id'    => $project_hashed_id,
        );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, "https://upload.wistia.com" );
    curl_setopt($ch, CURLOPT_UPLOAD, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 86400); // 1 Day Timeout
    curl_setopt($ch, CURLOPT_NOPROGRESS, false);
    curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
    curl_setopt($ch, CURLOPT_INFILESIZE, filesize($tmp_filename));
    curl_setopt($ch, CURLOPT_VERBOSE, 1); //for debugging
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    $result = curl_exec($ch);
    curl_close($ch);
    return json_decode($result);

UPDATE 2 (Working)

    $data = array(
        'api_password'  => $api_password,
        'file'          => '@'.$tmp_filename,
        'project_id'    => $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);

1 Answer 1

2

You don't need to open a handle for curl, just have

$data = array(
    'file' => '@move_file.MOV'
);

Curl will see the @ and treat it as a file upload attempt. You also don't have to do http_build_query() either. Curl can accept an array directly and do the query building itself:

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
Sign up to request clarification or add additional context in comments.

10 Comments

The docs from the API (wistia.com/doc/upload-api#the_request) state: "All parameters (with the exception of file) may be encoded into the request body or included as part of the query string. The file parameter must be multipart-form encoded into the request body." Is the "@" syntax compatible with this?
I think this is also necessary : curl_setopt($ch, CURLOPT_POST, 1);. Look here for more info.
+ Although clearly listed in the docs, I did not know about @. Looks like it's deprecated in PHP 5.5.
I just added an update which I think contains @redreggae and MarcB's fixes. It still doesn't work. Did I miss something?
@Emerson you should try it with the option CURLOPT_PROGRESSFUNCTION
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.