【问题标题】:How to convert curl file post from command line into PHP cURL如何将 curl 文件从命令行转换为 PHP cURL
【发布时间】:2013-07-25 19:21:15
【问题描述】:

我正在尝试从我的 PHP 应用程序将文件发布到第 3 方 API。 这可以从命令行工作:

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

但我无法使用 PHP 的 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);

我认为CURLOPT_INFILE 是问题所在,但我不确定。提前感谢您能给我的任何帮助。

更新1

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

更新 2(工作中)

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

【问题讨论】:

    标签: php post file-upload curl


    【解决方案1】:

    你不需要为curl打开一个句柄,只要有

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

    Curl 将看到@ 并将其视为文件上传尝试。你也不必做http_build_query()。 Curl 可以直接接受一个数组并自行构建查询:

    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    

    【讨论】:

    • API (wistia.com/doc/upload-api#the_request) 中的文档状态:“所有参数(文件除外)都可以编码到请求正文中或包含在查询字符串中。文件参数必须被多部分形式编码到请求正文中。” “@”语法与此兼容吗?
    • 我认为这也是必要的:curl_setopt($ch, CURLOPT_POST, 1);。查看here了解更多信息。
    • + 虽然在the docs 中明确列出,但我不知道@。看起来它在 PHP 5.5 中已被弃用。
    • 我刚刚添加了一个更新,我认为其中包含@redreggae 和 MarcB 的修复。它仍然不起作用。我错过了什么吗?
    • @Emerson 你应该试试CURLOPT_PROGRESSFUNCTION
    猜你喜欢
    • 1970-01-01
    • 2012-05-10
    • 2019-06-04
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 2014-08-28
    相关资源
    最近更新 更多