Skip to content

Commit 67bcd26

Browse files
author
Dani Sanchez
committed
Merge branch 'master' of https://github.com/php-curl-class/php-curl-class into php-curl-class-master
Conflicts: Curl.class.php
2 parents 202cd4e + 5ead0ee commit 67bcd26

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

Curl.class.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,12 @@ 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) {
88-
if (is_string($value) || is_int($value)) {
89-
$query[] = urlencode(is_null($key) ? $k : $key.'['.$k.']') . '=' . rawurlencode($value);
90+
if (is_string($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)) {
9295
$query[] = $this->http_build_multi_query($value, $k);
@@ -168,6 +171,10 @@ function __destruct() {
168171
public $response = NULL;
169172
}
170173

174+
function is_array_assoc($array) {
175+
return (bool)count(array_filter(array_keys($array), 'is_string'));
176+
}
177+
171178
function is_array_multidim($array) {
172179
if (!is_array($array)) {
173180
return FALSE;

tests/run.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@ public function testExtensionLoaded() {
1010
$this->assertTrue(extension_loaded('curl'));
1111
}
1212

13+
public function testArrayAssociative() {
14+
$this->assertTrue(is_array_assoc(array(
15+
'foo' => 'wibble',
16+
'bar' => 'wubble',
17+
'baz' => 'wobble',
18+
)));
19+
}
20+
21+
public function testArrayIndexed() {
22+
$this->assertFalse(is_array_assoc(array(
23+
'wibble',
24+
'wubble',
25+
'wobble',
26+
)));
27+
}
28+
1329
public function testUserAgent() {
1430
$test = new Test();
1531
$test->curl->setUserAgent(Curl::USER_AGENT);
@@ -43,6 +59,20 @@ public function testPostData() {
4359
)) === 'post');
4460
}
4561

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+
4676
public function testPostMultidimensionalData() {
4777
$test = new Test();
4878
$this->assertTrue($test->server('POST', array(

0 commit comments

Comments
 (0)