首页 文章

使用GuzzleHttp将视频上传到DailyMotion

提问于
浏览
0

我正在尝试使用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=pppppppppppppppppp`在400 Bad Request响应中导致:{“error”: “无效内容范围”,“封印”:“yyyyyyyyyyyyyyyyyyy”} /home/vagrant/Code/svc-titus/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:111

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

1 回答

  • 2

    我不认为你需要指定内容类型 Headers 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);
    

相关问题