我正在尝试将文件上传到我的服务器,然后将该文件发送到Zendesk。Zendesk文档展示了如何实现:
curl "https://{subdomain}.zendesk.com/api/v2/uploads.json?filename=myfile.dat&token={optional_token}" \
-v -u {email_address}:{password} \
-H "Content-Type: application/binary" \
--data-binary @file.dat -X POST
这个很好用。现在我需要使用Guzzle(版本6)重写它。 我正在使用Symfony 2.7:
$file = $request->files->get('file');
$urlAttachments = $this->params['base_url']."/api/v2/uploads.json?filename=".$file->getClientOriginalName();
$body = [
'auth' => [$this->params['user'], $this->params['pass']],
'multipart' => [
[
'name' => $archivo->getClientOriginalName(),
'contents' => fopen($file->getRealPath(), "r"),
],
]
];
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $urlAttachments, $body);
$response = json_decode($response->getBody(), true);
文件正在上传,但是当我下载它时,它的内容中也包含了一些元数据(破坏了其他文件类型)。我想我可能没有正确地上传它,因为使用curl命令上传可以正常工作。
--5b8003c370f19
Content-Disposition: form-data; name="test.txt"; filename="php6wiix1"
Content-Length: 1040
... The rest of the original content of the test file...
--5b8003c370f19--
我不知道为什么这些数据也被发送到文件中(我不想要它),或者使用multipart是否可以。
感谢任何帮助!