Guzzle〜6.0 multipart和form_params [英] Guzzle ~6.0 multipart and form_params

查看:763
本文介绍了Guzzle〜6.0 multipart和form_params的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试同时上传档案和传送讯息参数,例如:

I am trying to upload file and send post parameters at the same time like this:

$response = $client->post('http://example.com/api', [
    'form_params' => [
        'name' => 'Example name',
    ],
    'multipart' => [
        [
            'name'     => 'image',
            'contents' => fopen('/path/to/image', 'r')
        ]
    ]
]);

但是我的form_params字段被忽略,只有多部分字段存在于我的文章正文中。

However my form_params fields are ignored and only the multipart fields are present in my post body. Can I send both at all with guzzle 6.0 ?

推荐答案

我碰到了同样的问题。您需要将您的form_params添加到 multipart 数组中。其中name是表单元素名称,contents是值。您提供的示例代码将变为:

I ran into the same problem. You need to add your form_params to the multipart array. Where 'name' is the form element name and 'contents' is the value. The example code you supplied would become:

$response = $client->post('http://example.com/api', [
    'multipart' => [
        [
            'name'     => 'image',
            'contents' => fopen('/path/to/image', 'r')
        ],
        [
            'name'     => 'name',
            'contents' => 'Example name'
        ]
    ]
]);

这篇关于Guzzle〜6.0 multipart和form_params的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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