【问题标题】:CURL fail to send POST request with filestreamCURL 无法使用文件流发送 POST 请求
【发布时间】:2015-05-26 06:08:57
【问题描述】:

试图将文件流与其他数据一起发送到 API,但它返回错误:

The video provided was null

这是我的代码:

function create_video($files) {
    $api = "http://api.brightcove.com/services/post";

    $local_file_list = $files['file']['tmp_name'];

    foreach ($local_file_list as $local_file) {
        try {
            $fp = fopen($local_file, 'r');

            $ch = curl_init();

            if (FALSE === $ch) {
                throw new Exception('failed to initialize');
            }

            //"video":{"name":"test","shortDescription":"test","startDate":1432282741000,"endDate":null}
            $request = 'json={"params":{"encode_to":"MP4","create_multiple_renditions":"True","token":"' . WRITE_TOKEN . '"},"method":"create_video"}';

            curl_setopt($ch, CURLOPT_URL, $api);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_INFILE, $fp);
            curl_setopt($ch, CURLOPT_INFILESIZE, filesize($local_file));

            $content = curl_exec($ch);

            if (FALSE === $content) {
                throw new Exception(curl_error($ch), curl_errno($ch));
            }

            die(var_dump(json_decode($content)));

            return json_decode($content);
        } catch (Exception $e) {
            trigger_error(sprintf('Curl failed with error #%d: %s', $e->getCode(), $e->getMessage()), E_USER_ERROR);
        }
    }
}

有几点需要注意:

  1. CURL 不返回错误,所以可能不是 CURL 引起的
  2. filesize($local_file) 已返回正确的文件大小
  3. fopen($local_file, 'r');返回流类型内容,所以文件存在

如何解决这个问题?谢谢

API 参考:

https://docs.brightcove.com/en/video-cloud/media/references/reference.html#Video_Write

更新:

感谢您的帮助,但它现在返回另一个错误:

POST methods require valid JSON-RPC in the POST body, with "method" and "params" properties

我有一些想法,因为当我查看请求负载时,它与正确的负载略有不同:

当前我的版本:

------WebKitFormBoundary8VABz8KuNRE8Hepd
Content-Disposition: form-data; name="file[0]"; filename="big_buck_bunny.mp4"
Content-Type: video/mp4


------WebKitFormBoundary8VABz8KuNRE8Hepd--

正确的版本

------WebKitFormBoundaryCAB6WEANBJxoB3Op
Content-Disposition: form-data; name="JSONRPC"

{"params":{"video":{"name":"test","shortDescription":"test","startDate":1432282741000,"endDate":null},"encode_to":"MP4","create_multiple_renditions":"True","token":"VyocgALDnxU8HPvmnSnckgmXjoPlYWomc2La5Tn-evuAfsnSPJJoow.."},"method":"create_video"}
------WebKitFormBoundaryCAB6WEANBJxoB3Op
Content-Disposition: form-data; name="filePath"; filename="big_buck_bunny.mp4"
Content-Type: video/mp4


------WebKitFormBoundaryCAB6WEANBJxoB3Op

    Content-Disposition: form-data; name="JSONView"

    {"params":{"video":{"name":"test","shortDescription":"test","startDate":1432282741000,"endDate":null},"encode_to":"MP4","create_multiple_renditions":"True","token":"VyocgALDnxU8HPvmnSnckgmXjoPlYWomc2La5Tn-evuAfsnSPJJoow.."},"method":"create_video"}
    ------WebKitFormBoundaryCAB6WEANBJxoB3Op--

【问题讨论】:

    标签: php file post networking curl


    【解决方案1】:

    您可以将要上传的文件打包成CURLFile (PHP >= 5.5):

    $cfile = new CURLFile($local_file, 'video/mp4', basename($local_file));
    

    然后,添加到CURLOPT_POSTFIELDS,使其成为整个请求的一部分:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POSTFIELDS, [
        'file' => $cfile,
        'JSONRPC' => '{"params":{"encode_to":"MP4","create_multiple_renditions":"True","token":"' . WRITE_TOKEN . '"},"method":"create_video"}';
    ]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    $content = curl_exec($ch);
    

    在 PHP 5.5 之前,您可以使用 @ 来指示文件:

    curl_setopt($ch, CURLOPT_POSTFIELDS, [
        'file' => "@$local_file",
        'JSONRPC' => '{"params":{"encode_to":"MP4","create_multiple_renditions":"True","token":"' . WRITE_TOKEN . '"},"method":"create_video"}';
    ]);
    

    【讨论】:

    • @user782104 您也可以将其留空,但如果您对它应该是什么有很好的了解,请使用它。你不应该使用通配符。
    • @user782104 当然,您可以改用'file' => "@$local_file",
    • @user782104 好吧,这不再是 PHP 问题了。
    • @user782104 也许您没有使用正确的命名?试试JSONRPC 而不是json
    • @user782104 那么,也许 JSON 格式错误?为什么不使用json_encode() 呢?您可以尝试的另一件事是切换帖子字段的顺序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-14
    • 2012-08-31
    • 1970-01-01
    • 1970-01-01
    • 2016-10-16
    • 2020-12-29
    • 2016-05-12
    相关资源
    最近更新 更多