且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将curl文件从命令行转换为PHP cURL

更新时间:2023-02-16 19:55:37

,只要有

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

Curl会看到 @ 作为文件上传尝试。你也不必做 http_build_query()。 curl可以直接接受一个数组,并且可以构建自己的查询:
$ b $ $ $ p $ curl_setopt($ ch,CURLOPT_POSTFIELDS,$ data);


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

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