我有一个可以将视频上传并发送到远程目标的表格。我有一个cURL请求,我想使用Guzzle将其“翻译”为PHP。
到目前为止,我有这个:
public function upload(Request $request)
{
$file = $request->file('file');
$fileName = $file->getClientOriginalName();
$realPath = $file->getRealPath();
$client = new Client();
$response = $client->request('POST', 'http://mydomain.de:8080/spots', [
'multipart' => [
[
'name' => 'spotid',
'country' => 'DE',
'contents' => file_get_contents($realPath),
],
[
'type' => 'video/mp4',
],
],
]);
dd($response);
}
Run Code Online (Sandbox Code Playgroud)
这是我要使用的cURL,并想翻译成PHP:
curl -X POST -F 'body={"name":"Test","country":"Deutschland"};type=application/json' -F 'file=@C:\Users\PROD\Downloads\617103.mp4;type= video/mp4 ' http://mydomain.de:8080/spots
Run Code Online (Sandbox Code Playgroud)
因此,当我上传视频时,我想替换此硬编码
C:\ Users \ PROD \ Downloads \ 617103.mp4。
运行此命令时,出现错误:
客户端错误:
POST http://mydomain.de:8080/spots导致400 Bad Request响应:请求正文无效:期望表单值“ body”客户端错误:
POST http://mydomain.de/spots导致400 Bad Request响应:请求正文无效:期望表单值“ body”
我将审查Guzzle的multipart请求选项。我看到两个问题:
body)进行传递。的type在卷曲请求映射到报头Content-Type。来自$ man curl:
您还可以通过使用'type ='来告诉curl使用什么Content-Type。
尝试类似:
$response = $client->request('POST', 'http://mydomain.de:8080/spots', [
'multipart' => [
[
'name' => 'body',
'contents' => json_encode(['name' => 'Test', 'country' => 'Deutschland']),
'headers' => ['Content-Type' => 'application/json']
],
[
'name' => 'file',
'contents' => fopen('617103.mp4', 'r'),
'headers' => ['Content-Type' => 'video/mp4']
],
],
]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2966 次 |
| 最近记录: |