php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #67585 curl_setopt POSTFIELDS doesn't handle nested arrays as it should
Submitted: 2014-07-07 14:49 UTC Modified: 2014-07-29 01:37 UTC
Votes:4
Avg. Score:3.5 ± 1.7
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:1 (33.3%)
From: zelnaga at gmail dot com Assigned:
Status: Open Package: cURL related
PHP Version: 5.6.0RC2 OS: Windows 7
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2014-07-07 14:49 UTC] zelnaga at gmail dot com
Description:
------------
<?php
$data = array(
	'submit' => 'submit',
	'var' => array(1, 2)
);

echo http_build_query($data, '', '&');
?>

That'll output the following:

submit=submit&var%5B0%5D=1&var%5B1%5D=1

It seems like curl_setopt(..., CURLOPT_POSTFIELDS, $data) ought to behave similarly but it doesn't.



Test script:
---------------
<?php

$data = array(
	'submit' => 'submit',
	'var' => array(1, 2)
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.domain.tld/script.php');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

echo curl_exec($ch);

Expected result:
----------------
Array
(
    [submit] => submit
    [var] => Array
)

Actual result:
--------------
Array
(
    [submit] => submit
    [var] => Array
        (
            [0] => 1
            [1] => 2
        )

)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-07-29 01:37 UTC] zelnaga at gmail dot com
I accidentally swapped the expected output and the actual output when reporting this bug. FYI.
 [2016-02-19 05:19 UTC] sergey at geekytomato dot com
I experience the same issue on 5.6.18 (Ubuntu 15.10)

In the following example $data['key2'] becomes the "Array" string:
---
$curl = curl_init();
$data = [ 'key1' => 'val1', 'key2' => [ 'subkey1' => 'subval1', 'subkey2' => 'subval2' ] ];
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
var_dump($data);
---
Notice: Array to string conversion in /var/www/sites/test/index.php on line 5
array(2) {
  ["key1"]=>
  string(4) "val1"
  ["key2"]=>
  string(5) "Array"
}
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Tue Aug 29 15:01:52 2017 UTC