donglijuan8227 2016-12-06 05:15
浏览 148
已采纳

使用GuzzleHttp将视频上传到DailyMotion

I'm trying to upload a video using Laravel and GuzzleHttp to DailyMotion. Here's my code:

$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 is a dynamically generated URL passed by DailyMotion. Upon executing the code above, I'll always get this error:

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

But I can upload video to the same upload URL using Postman, as displayed below: enter image description here

  • 写回答

1条回答 默认 最新

  • duanqinqiao4844 2016-12-06 05:30
    关注

    i don't think you need to specify content-type header guzzle will decide it automatically when you provide it a resource also the path of your video here seems problematic if video is stored at public directory you need to use public_path or respective path helper function to get its physical path below should work in guzzleHttp 6 check sending form files here 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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 爬取1-112页所有帖子的标题但是12页后要登录后才能 我使用selenium模拟登录 账号密码输入后 会报错 不知道怎么弄了
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点