php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #51634 Can't post multiple fields with the same name
Submitted: 2010-04-22 15:06 UTC Modified: 2022-04-07 09:03 UTC
Votes:37
Avg. Score:4.3 ± 1.2
Reproduced:30 of 31 (96.8%)
Same Version:9 (30.0%)
Same OS:12 (40.0%)
From: bart at tremby dot net Assigned: derick (profile)
Status: Closed Package: cURL related
PHP Version: 7.1 OS:
Private report: No CVE-ID: None
 [2010-04-22 15:06 UTC] bart at tremby dot net
Description:
------------
With CLI curl I can run
curl -F test=value -F test=value --trace-ascii trace http://localhost/test.php
and in the file trace I see that it posted multipart/form-data with two fields called "test" with content "value".

I need this same behaviour from PHP. But the only way at present, it seems, to add form fields to the curl handle (and have them transmit as multipart/form-data) is to use curl_setopt($ch, CURLOPT_POSTFIELDS, $data) where $data is an array of name->value pairs.

Obviously I can't have two pairs in this array with the same name.

I've tried array("name" => array("val1", "val2")) but that posts the string "Array" as the value for field "name".

I've tried array("name[]" => array("val1", "val2")) but that posts the string "Array" as the value for field "name[]" (PHP then parses this into an array but only "val2" is in it.)

I've tried array("name[]" => "val1", "name[]" => "val2") but of course that doesn't work since as soon as that array is initialized it's only got one element -- the second overwrote the first.

I think allowing array("name" => array("val1", "val2")) would be the best solution. (And brackets should not be added to the end of "name" unless specified.)


Patches

51634-curl-multivalued-field-post.patch (last revision 2012-05-14 20:12 UTC by raphael dot droz+floss at gmail dot com)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-04-22 15:07 UTC] bart at tremby dot net
Where I said 'PHP then parses this into an array but only "val2" is in it.' I meant "Array", not "val2".
 [2010-04-22 15:53 UTC] felipe@php.net
You can use: array("name[0]" => "val1", "name[1]" => "val2")
 [2010-04-22 16:01 UTC] bart at tremby dot net
That works when posting to PHP because of the way PHP handles the names but what it's actually posting is
name "name[0]", value "val1"
name "name[1]", value "val2"

Any system but PHP as far as I know (I'm posting to a Java-based system and I don't have control over it) keeps them as they are -- two separate entities called "name[0]" and "name[1]". There's nothing in the HTTP specification which says anything about array indices -- as far as I can tell that's purely PHP's invention. PHP which decides they're the same thing and knocks off the brackets.

The system I'm posting to expects them to be posted with the same name. (The spec says this is fine -- see <http://www.w3.org/MarkUp/html-spec/html-spec_8.html#SEC8.1.2.3>). The CLI example I gave in the OP does this; the PHP example you just gave does not.
 [2012-05-14 20:21 UTC] raphael dot droz+floss at gmail dot com
This patch may solve the issue.

I can't see how may I have restricted the new code-path according to the key so I simply tested for values which are array themselves instead of doing any string lookup/substitution when keys matches "[]" or anything like that.

I got some problem with the test-suite (can't export PHP_CURL_HTTP_REMOTE_SERVER) anyway I still had 40 skipped, 2 failures and 33 pass *after* the patch.
If that patch is ok I may add a patch for the test-suite if needed.

NB: please don't bite me, it's my first patch for PHP.
 [2014-04-25 08:57 UTC] mike@php.net
-Assigned To: +Assigned To: mike
 [2016-08-22 12:57 UTC] mike@php.net
-Status: Assigned +Status: Open -Assigned To: mike +Assigned To:
 [2018-04-13 08:23 UTC] requinix@php.net
-Type: Bug +Type: Feature/Change Request -Operating System: Ubuntu +Operating System: -PHP Version: 5.2.13 +PHP Version: 7.1
 [2018-04-13 08:23 UTC] requinix@php.net
Reviving as a feature request.

I agree that the implementation should not append []s automatically and instead require the developer to use the right name with the array key. That would allow support for languages like .NET which use multiple key= pairs.
 [2019-09-15 22:18 UTC] randallfstewart at gmail dot com
I wanted to add that the current approach (simple name-value POSTs stored as array element indexes and array element values) fails for HTTP Multipart/Form-Data file uploads.  The RFC actually requires that multiple file uploads from the same field all use the same name.  See section 4.3 of RFC 7578:

"To match widely deployed implementations, multiple files MUST be sent by supplying each file in a separate part but all with the same 'name' parameter."

I came across this while trying to use the mailgun API with multiple attachments.  The mailgun API, in accordance with RFC 7578, requires all file uploads to use the same POST name "attachment."  When using PHP cURL, this limits me to a single attachment.
 [2019-12-15 14:22 UTC] psufan513 at aol dot com
I don't understand why having PHP comply with RFC is a feature request? Why hasn't this BUG been fixed yet? I am also having this issue trying to implement Mailgun which requires multiple "to" fields per recipient. Using Windows 10 / PHP 7.2.10.
 [2022-03-22 19:38 UTC] neuner at futureweb dot at
Stumbled over this problem too. Too bad that hasn't been fixed :( I don't get the API to change their behavior, as I am not in control of it.
 [2022-04-01 16:03 UTC] derick@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: derick
 [2022-04-01 16:03 UTC] derick@php.net
https://github.com/php/php-src/pull/8292 for PHP 8.2.
 [2022-04-07 09:03 UTC] derick@php.net
-Status: Assigned +Status: Closed
 [2022-04-07 09:03 UTC] derick@php.net
The fix for this bug has been committed.
If you are still experiencing this bug, try to check out latest source from https://github.com/php/php-src and re-test.
Thank you for the report, and for helping us make PHP better.

For PHP 8.2.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 21:01:28 2024 UTC