使用GuzzleHttp将视频上传到DailyMotion

时间:2016-12-06 05:15:28

标签: php laravel guzzle dailymotion-api

我尝试使用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:
  客户端错误:   POST   http://upload-02.sg1.dailymotion.com/upload?uuid=werewkrewrewrwer&seal=pppppppppppppppp`导致   在400 Bad Request响应中:
      {&#34;错误&#34;:&#34;无效的内容范围&#34;,&#34;封印&#34;:&#34; yyyyyyyyyyyyyyyyyyy&#34;}        在/home/vagrant/Code/svc-titus/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:111

但我可以使用Postman将视频上传到同一个上传网址,如下所示: enter image description here

1 个答案:

答案 0 :(得分:2)

我认为你不需要指定内容类型标题guzzle会在你提供资源时自动决定它也是你的视频路径似乎有问题如果视频存储在公共目录你需要使用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);