PHP狂饮.如何为多部分POST请求设置自定义边界? [英] PHP Guzzle. How to set custom boundary to the multipart POST request?

查看:106
本文介绍了PHP狂饮.如何为多部分POST请求设置自定义边界?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为多部分POST请求设置自定义边界?遵循请求选项配置不起作用.

How can I set custom boundary to the multipart POST request? Following request options configuration doesn't work.

'headers' => ['Content-Type' => 'multipart/form-data; boundary=CUSTOM_BOUNDARY']

推荐答案

Guzzle使用psr7将多部分表单字段组合到请求主体中.处理自定义边界的最正确方法是使用 GuzzleHttp \ Psr7 \ MultipartStream .

Guzzle uses psr7 to combine multipart form fields into request body. The most correct way to deal with custom boundary would be using GuzzleHttp\Psr7\MultipartStream.

$boundary = 'my_custom_boundary';
$multipart_form = [
    [
        'name' => 'upload_id',
        'contents' => $upload_id,
    ],
    [
        'name' => '_uuid',
        'contents' => $uuid,
    ],
    ...
];

$params = [
    'headers' => [
        'Connection' => 'close',
        'Content-Type' => 'multipart/form-data; boundary='.$boundary,
    ],
    'body' => new GuzzleHttp\Psr7\MultipartStream($multipart_form, $boundary), // here is all the magic
];

$res = $this->client->request($method, $url, $params);

这篇关于PHP狂饮.如何为多部分POST请求设置自定义边界?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆