PHP Guzzle. How to set custom boundary to the multipart POST request?

前端 未结 2 1307
傲寒
傲寒 2021-01-24 18:40

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

\'headers\' => [\'Content-Type\' => \'m         


        
2条回答
  •  北海茫月
    2021-01-24 19:13

    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);
    

提交回复
热议问题