Skip to content

Commit 3fbaf42

Browse files
authored
feat: Build URL with multiple instances of the same param (#136)
1 parent 1cf7f4d commit 3fbaf42

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

USAGE.md

+17
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@ echo $response->body();
5050
echo $response->headers();
5151
```
5252

53+
#### GET with array of values
54+
55+
```
56+
$query_params = [
57+
'aggregated_by' => 'month',
58+
'subusers' => ['one', 'two', 'three'],
59+
'start_date' => '2019-01-01',
60+
'end_date' => '2019-01-31',
61+
];
62+
$request_headers = ['X-Mock: 200'];
63+
$retryOnLimit = true;
64+
$response = $client->subusers()->stats()->get(null, $query_params, $request_headers, $retryOnLimit);
65+
echo $response->statusCode();
66+
echo $response->body();
67+
echo $response->headers();
68+
```
69+
5370
<a name="delete"></a>
5471
## DELETE
5572

lib/Client.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,16 @@ public function setIsConcurrentRequest($isConcurrent)
346346
*
347347
* @param array $queryParams an array of all the query parameters
348348
*
349+
* Nested arrays will resolve to multiple instances of the same parameter
350+
*
349351
* @return string
350352
*/
351353
private function buildUrl($queryParams = null)
352354
{
353355
$path = '/' . implode('/', $this->path);
354356
if (isset($queryParams)) {
355-
$path .= '?' . http_build_query($queryParams);
357+
// Regex replaces `[0]=`, `[1]=`, etc. with `=`.
358+
$path .= '?' . preg_replace('/%5B(?:\d|[1-9]\d+)%5D=/', '=', http_build_query($queryParams));
356359
}
357360

358361
return sprintf('%s%s%s', $this->host, $this->version ?: '', $path);

test/unit/ClientTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,22 @@ public function testMakeRequestWithUntrustedRootCert()
218218
$client->makeRequest('GET', 'https://untrusted-root.badssl.com/');
219219
}
220220

221+
public function testFormRepeatUrlArgs()
222+
{
223+
$client = new Client('https://localhost:4010');
224+
225+
$testParams = [
226+
'thing' => 'stuff',
227+
'foo' => [
228+
'bar',
229+
'bat',
230+
'baz',
231+
],
232+
];
233+
$result = $this->callMethod($client, 'buildUrl', [$testParams]);
234+
$this->assertEquals($result, 'https://localhost:4010/?thing=stuff&foo=bar&foo=bat&foo=baz');
235+
}
236+
221237
/**
222238
* @param object $obj
223239
* @param string $name

0 commit comments

Comments
 (0)