File tree Expand file tree Collapse file tree 2 files changed +39
-2
lines changed Expand file tree Collapse file tree 2 files changed +39
-2
lines changed Original file line number Diff line number Diff line change @@ -84,9 +84,12 @@ function close() {
84
84
function http_build_multi_query ($ data , $ key =NULL ) {
85
85
$ query = array ();
86
86
87
+ $ is_array_assoc = is_array_assoc ($ data );
88
+
87
89
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 );
90
93
}
91
94
else if (is_array ($ value )) {
92
95
$ query [] = $ this ->http_build_multi_query ($ value , $ k );
@@ -168,6 +171,10 @@ function __destruct() {
168
171
public $ response = NULL ;
169
172
}
170
173
174
+ function is_array_assoc ($ array ) {
175
+ return (bool )count (array_filter (array_keys ($ array ), 'is_string ' ));
176
+ }
177
+
171
178
function is_array_multidim ($ array ) {
172
179
if (!is_array ($ array )) {
173
180
return FALSE ;
Original file line number Diff line number Diff line change @@ -10,6 +10,22 @@ public function testExtensionLoaded() {
10
10
$ this ->assertTrue (extension_loaded ('curl ' ));
11
11
}
12
12
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
+
13
29
public function testUserAgent () {
14
30
$ test = new Test ();
15
31
$ test ->curl ->setUserAgent (Curl::USER_AGENT );
@@ -43,6 +59,20 @@ public function testPostData() {
43
59
)) === 'post ' );
44
60
}
45
61
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
+
46
76
public function testPostMultidimensionalData () {
47
77
$ test = new Test ();
48
78
$ this ->assertTrue ($ test ->server ('POST ' , array (
You can’t perform that action at this time.
0 commit comments