简体   繁体   中英

CURL fail to send POST request with filestream

Trying to send filestream with other data to an API , but it return the error:

The video provided was null

Here is my code:

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

Few things need to note:

  1. CURL does not return error , so it may not caused by CURL
  2. The filesize($local_file) has return the correct file size
  3. fopen($local_file, 'r'); return the stream type content, so the file is exist

How to fix that? Thanks

API reference:

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

Update:

Thanks for helping , but it now return another error:

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

I have some idea of that as when I look at the request payload, it is slightly different from the correct one:

Current my version:

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


------WebKitFormBoundary8VABz8KuNRE8Hepd--

The correct version

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

You can wrap the file to upload into CURLFile (PHP >= 5.5):

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

Then, add to CURLOPT_POSTFIELDS , making it part of the whole request:

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

Before PHP 5.5 you can use the @ to indicate a file:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM