小编mon*_*ode的帖子

用 Guzzle 重写 curl(文件上传) - PHP

我正在尝试将文件上传到我的服务器,然后将该文件发送到 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
Run Code Online (Sandbox Code Playgroud)

这工作正常。我现在必须用 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);
Run Code Online (Sandbox Code Playgroud)

该文件正在上传,但是当我下载它时,它还会在其内容中获取一些元数据(破坏其他一些文件类型)。我想我没有正确上传它,因为卷发的另一种方式工作正常。

--5b8003c370f19
Content-Disposition: form-data; name="test.txt"; filename="php6wiix1"
Content-Length: 1040

... The rest of the original content of the …
Run Code Online (Sandbox Code Playgroud)

php curl guzzle symfony-2.7 zendesk-api

2
推荐指数
1
解决办法
2526
查看次数

标签 统计

curl ×1

guzzle ×1

php ×1

symfony-2.7 ×1

zendesk-api ×1