diff --git a/USAGE.md b/USAGE.md index abf346a..d979639 100644 --- a/USAGE.md +++ b/USAGE.md @@ -50,6 +50,23 @@ echo $response->body(); echo $response->headers(); ``` +#### GET with array of values + +``` +$query_params = [ + 'aggregated_by' => 'month', + 'subusers' => ['one', 'two', 'three'], + 'start_date' => '2019-01-01', + 'end_date' => '2019-01-31', +]; +$request_headers = ['X-Mock: 200']; +$retryOnLimit = true; +$response = $client->subusers()->stats()->get(null, $query_params, $request_headers, $retryOnLimit); +echo $response->statusCode(); +echo $response->body(); +echo $response->headers(); +``` + ## DELETE diff --git a/lib/Client.php b/lib/Client.php index b15595c..bf36535 100644 --- a/lib/Client.php +++ b/lib/Client.php @@ -346,13 +346,16 @@ public function setIsConcurrentRequest($isConcurrent) * * @param array $queryParams an array of all the query parameters * + * Nested arrays will resolve to multiple instances of the same parameter + * * @return string */ private function buildUrl($queryParams = null) { $path = '/' . implode('/', $this->path); if (isset($queryParams)) { - $path .= '?' . http_build_query($queryParams); + // Regex replaces `[0]=`, `[1]=`, etc. with `=`. + $path .= '?' . preg_replace('/%5B(?:\d|[1-9]\d+)%5D=/', '=', http_build_query($queryParams)); } return sprintf('%s%s%s', $this->host, $this->version ?: '', $path); diff --git a/test/unit/ClientTest.php b/test/unit/ClientTest.php index 0d829a4..41f2ca8 100644 --- a/test/unit/ClientTest.php +++ b/test/unit/ClientTest.php @@ -218,6 +218,22 @@ public function testMakeRequestWithUntrustedRootCert() $client->makeRequest('GET', 'https://untrusted-root.badssl.com/'); } + public function testFormRepeatUrlArgs() + { + $client = new Client('https://localhost:4010'); + + $testParams = [ + 'thing' => 'stuff', + 'foo' => [ + 'bar', + 'bat', + 'baz', + ], + ]; + $result = $this->callMethod($client, 'buildUrl', [$testParams]); + $this->assertEquals($result, 'https://localhost:4010/?thing=stuff&foo=bar&foo=bat&foo=baz'); + } + /** * @param object $obj * @param string $name