Skip to content

Commit 5ead0ee

Browse files
committed
Fix php-curl-class#4: Arrays inside the data array loses its indexes
1 parent db0ddc4 commit 5ead0ee

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

Curl.class.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,15 @@ function close() {
8484
function http_build_multi_query($data, $key=NULL) {
8585
$query = array();
8686

87+
$is_array_assoc = is_array_assoc($data);
88+
8789
foreach ($data as $k => $value) {
8890
if (is_string($value)) {
89-
$query[] = urlencode(is_null($key) ? $k : $key) . '=' . rawurlencode($value);
91+
$brackets = $is_array_assoc ? '[' . $k . ']' : '[]';
92+
$query[] = urlencode(is_null($key) ? $k : $key . $brackets) . '=' . rawurlencode($value);
9093
}
9194
else if (is_array($value)) {
92-
$query[] = $this->http_build_multi_query($value, $k . '[]');
95+
$query[] = $this->http_build_multi_query($value, $k);
9396
}
9497
}
9598

tests/run.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ public function testPostData() {
5959
)) === 'post');
6060
}
6161

62+
public function testPostAssociativeArrayData() {
63+
$test = new Test();
64+
$this->assertTrue($test->server('POST', array(
65+
'test' => 'post_multidimensional',
66+
'username' => 'myusername',
67+
'password' => 'mypassword',
68+
'more_data' => array(
69+
'param1' => 'something',
70+
'param2' => 'other thing',
71+
'param3' => '123',
72+
),
73+
)) === 'test=post_multidimensional&username=myusername&password=mypassword&more_data%5Bparam1%5D=something&more_data%5Bparam2%5D=other%20thing&more_data%5Bparam3%5D=123');
74+
}
75+
6276
public function testPostMultidimensionalData() {
6377
$test = new Test();
6478
$this->assertTrue($test->server('POST', array(

0 commit comments

Comments
 (0)