2016-12-06 105 views
0

我试图使用Laravel和GuzzleHttp将视频上传到DailyMotion。以下是我的代码:使用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

但我可以上传视频到邮差使用相同的上传网址,如下面显示: enter image description here

回答

2

我不认为你需要指定内容类型的头文件guzzle会在您提供资源时自动决定它的资源,如果视频存储在公共目录中,则您的视频的路径似乎有问题,您需要使用public_path或相应的路径帮助程序函数来获取其物理路径 下面应该在guzzle中工作https 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); 
+0

我试图运行你的代码“,当这个错误production.ERROR:Symfony的\分量\调试\异常\ FatalThrowableError:类型错误:传递给GuzzleHttp \ PSR7 \ MultipartStream :: addElement方法(参数2)必须是给出的字符串,在第70行调用/home/vagrant/Code/svc-titus/vendor/guzzlehttp/psr7/src/MultipartStream.php“。基本上引发错误的行是'contents'= > fopen(base_path($ file),'r') – imin

+0

对不起,我的工作很糟糕。非常感谢! – imin