Bug #67585 [Com]: curl_setopt POSTFIELDS doesn't handle nested arrays as it should

From: Date: Fri, 19 Feb 2016 05:19:40 +0000
Subject: Bug #67585 [Com]: curl_setopt POSTFIELDS doesn't handle nested arrays as it should
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-199333@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=67585&edit=1

 ID:                 67585
 Comment by:         sergey at geekytomato dot com
 Reported by:        zelnaga at gmail dot com
 Summary:            curl_setopt POSTFIELDS doesn't handle nested arrays
                     as it should
 Status:             Open
 Type:               Bug
 Package:            cURL related
 Operating System:   Windows 7
 PHP Version:        5.6.0RC2
 Block user comment: N
 Private report:     N

 New Comment:

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"
}


Previous Comments:
------------------------------------------------------------------------
[2014-07-29 01:37:27] zelnaga at gmail dot com

I accidentally swapped the expected output and the actual output when reporting this bug. FYI.

------------------------------------------------------------------------
[2014-07-07 14:49:38] 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
        )

)


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=67585&edit=1


Thread (4 messages)

« previous php.bugs (#199333) next »