Bug->Req #76216 [Opn]: CURLOPT_POSTFIELDS should support key[] syntax for array values
| From: | requinix@php.net | Date: | Fri, 13 Apr 2018 08:20:02 +0000 |
| Subject: | Bug->Req #76216 [Opn]: CURLOPT_POSTFIELDS should support key[] syntax for array values | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-214724@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=76216&edit=1
ID: 76216
Updated by: requinix@php.net
Reported by: standa dot david at gmail dot com
-Summary: Post not working with subarray and file
+Summary: CURLOPT_POSTFIELDS should support key[] syntax for
array values
Status: Open
-Type: Bug
+Type: Feature/Change Request
Package: cURL related
Operating System: Debian
PHP Version: 7.2.4
Block user comment: N
Private report: N
New Comment:
Duplicate of bug #51634, #67585, and others.
Workaround:
Normally constructing the POST data as a string would be easy, but that doesn't work for
multipart bodies.
Use var[0], var[1], etc. as array keys.
$postdata = array(
'file'=> $curlfile,
'var[0]' => 'value1',
'var[1]' => 'value2'
);
It's not literally the same but if you have PHP on the receiving end too then it won't
make a difference.
Previous Comments:
------------------------------------------------------------------------
[2018-04-13 07:49:10] standa dot david at gmail dot com
Description:
------------
do you have any idea, how to post file with subarray form fields
Simple example: I need to have in form multiple var[] variables. So I build post array:
$curlfile = curl_file_create($filename);
$postdata = array('file'=> $curlfile,
'var' => array('value1', 'value2')
);
curl_setopt($ch,CURLOPT_POSTFIELDS, $postdata);
Then I will make simple post request, but it is not working. When I inspect the result with https://httpbin.org/post or print_r($_POST) variable on my site,
it will show only
Array
(
[var] => Array
)
I need to have in post request var[]=value1&var[]=value2
Test script:
---------------
$curlfile = curl_file_create($filename);
$postdata = array('file'=> $curlfile,
'var' => array('value1', 'value2')
);
$ch = curl_init ("https://httpbin.org/post");
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
Expected result:
----------------
"form": {
"var[]": [
"value1",
"value2"
]
},
Actual result:
--------------
"form": {
"var": "Array",
},
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=76216&edit=1