php Guzzle ~6.0 multipart 和 form_params
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30645996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Guzzle ~6.0 multipart and form_params
提问by Jordan Dobrev
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')
]
]
]);
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 字段被忽略,我的帖子正文中只存在多部分字段。我可以用 guzzle 6.0 发送两者吗?
回答by Simon Crowfoot
I ran into the same problem. You need to add your form_params to the multipartarray. Where 'name' is the form element name and 'contents' is the value. The example code you supplied would become:
我遇到了同样的问题。您需要将 form_params 添加到多部分数组中。其中“name”是表单元素名称,“contents”是值。您提供的示例代码将变为:
$response = $client->post('http://example.com/api', [
'multipart' => [
[
'name' => 'image',
'contents' => fopen('/path/to/image', 'r')
],
[
'name' => 'name',
'contents' => 'Example name'
]
]
]);
回答by Jordan Dobrev
I got there too, but unfortunately it does not work if you have multidimensional params array. The only way i got it to work is if you send the form_paramaters as query parameters in the array:
我也到了那里,但不幸的是,如果您有多维参数数组,它就不起作用。我让它工作的唯一方法是,如果您将 form_paramaters 作为查询参数发送到数组中:
$response = $client->post('http://example.com/api', [
'query' => [
'name' => 'Example name',
],
'multipart' => [
[
'name' => 'image',
'contents' => fopen('/path/to/image', 'r')
]
]
]);