【发布时间】:2016-04-04 16:38:50
【问题描述】:
我必须用文件发送数组数据。仅使用数据可以正常工作:
$client->post('http://xxx/', [
'form_params' => [
[
'data' => ['id' => 1234, 'name' => 'nombre'],
'valid' => true
]
]
]);
但由于我不能将“form_params”与“multipart”一起使用,如何发送带有数组和布尔数据的文件?
我试过了:
$client->post('http://xxx/', [
'multipart' => [
[
'name' => 'myfile',
'contents' => fopen('my_file.txt', 'r'),
],
[
'name' => 'data',
'contents' => ['id' => 1234, 'name' => 'nombre'],
]
[
'name' => 'valid',
'contents' => true,
]
],
]);
但我收到一个错误,因为“内容”不接受布尔值或数组值。
我需要一些帮助。
谢谢
更新: 我无法解决问题,最后我不得不使用一个不太好的解决方案,包括作为查询字符串的表单字段参数并仅使用 Multipart。比如:
$client->post('http://xxx?id=1234&name=nombre', [
'multipart' => [
[
'name' => 'myfile',
'contents' => fopen('my_file.txt', 'r'),
],
],
]);
【问题讨论】:
-
我自己也在解决这个问题。我通过设置 [headers] => [ [Content-Type] => application/x-www-form-urlencoded ] 获得了数组数据。但我无法让文件与这些数据一起传递。看到这个关于数组作为内容值的问题github.com/guzzle/guzzle/issues/1177