使用 GuzzleHttp 将视频上传到 DailyMotion

upload video to DailyMotion using GuzzleHttp

我正在尝试使用 Laravel 和 GuzzleHttp 将视频上传到 DailyMotion。这是我的代码:

$file = "3.mp4";
$fields["file"] = fopen($file, 'rb');
$res = $client->post($upload_url, [
   'headers' => ['Content-Type' => 'multipart/form-data'],
    $fields
]);

$data3 =  $res->getBody();
$response_upload_video = json_decode($data3,true);
echo "<br>Getting dm upload video response: ";
print_r($response_upload_video);

$upload_url是DailyMotion动态生成的URL。执行上面的代码后,我总是会得到这个错误:

Production.ERROR: GuzzleHttp\Exception\ClientException:
Client error: POST http://upload-02.sg1.dailymotion.com/upload?uuid=werewkrewrewrwer&seal=pppppppppppppppp`resulted in a 400 Bad Request response:
{"error":"invalid content range","seal":"yyyyyyyyyyyyyyyyyy"} in /home/vagrant/Code/svc-titus/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:111

但是我可以使用Postman将视频上传到同一个上传URL,如下图:

我不认为你需要指定 content-type header guzzle 会在你提供资源时自动决定它,如果视频,你的视频路径在这里似乎有问题存储在 public 目录中,您需要使用 public_path 或相应的路径辅助函数来获取其物理路径 下面应该在 guzzleHttp 6 中工作,请在此处检查发送表单文件 http://docs.guzzlephp.org/en/latest/quickstart.html#uploading-data

$file = "3.mp4";
$res = $client->post($upload_url, [
     'multipart' => [
        [
            'name'     => 'file',
            'contents' => fopen(base_path($file), 'r') // give absolute path using path helper function 
        ]
    ]
]);

$data3 =  $res->getBody();
$response_upload_video = json_decode($data3,true);
echo "<br>Getting dm upload video response: ";
print_r($response_upload_video);