diff --git a/Curl.class.php b/Curl.class.php index 1e2c0fe..2088253 100644 --- a/Curl.class.php +++ b/Curl.class.php @@ -84,12 +84,15 @@ class Curl { function http_build_multi_query($data, $key=NULL) { $query = array(); + $is_array_assoc = is_array_assoc($data); + foreach ($data as $k => $value) { if (is_string($value)) { - $query[] = urlencode(is_null($key) ? $k : $key) . '=' . rawurlencode($value); + $brackets = $is_array_assoc ? '[' . $k . ']' : '[]'; + $query[] = urlencode(is_null($key) ? $k : $key . $brackets) . '=' . rawurlencode($value); } else if (is_array($value)) { - $query[] = $this->http_build_multi_query($value, $k . '[]'); + $query[] = $this->http_build_multi_query($value, $k); } } diff --git a/tests/run.php b/tests/run.php index d7288fb..320d395 100644 --- a/tests/run.php +++ b/tests/run.php @@ -59,6 +59,20 @@ class CurlTest extends PHPUnit_Framework_TestCase { )) === 'post'); } + public function testPostAssociativeArrayData() { + $test = new Test(); + $this->assertTrue($test->server('POST', array( + 'test' => 'post_multidimensional', + 'username' => 'myusername', + 'password' => 'mypassword', + 'more_data' => array( + 'param1' => 'something', + 'param2' => 'other thing', + 'param3' => '123', + ), + )) === 'test=post_multidimensional&username=myusername&password=mypassword&more_data%5Bparam1%5D=something&more_data%5Bparam2%5D=other%20thing&more_data%5Bparam3%5D=123'); + } + public function testPostMultidimensionalData() { $test = new Test(); $this->assertTrue($test->server('POST', array(