Skip to content

Commit 4522e85

Browse files
committed
Fixed a bug which was introduced when switching to http_build_query for
query string encoding where repeated parameters would adopt named indexes. Library now defaults to omitting numeric indexes, which can be re-enabled with the `build_indexed_queries` boolean option. Original tests now pass, additional tests added to verify integrity of regex hack.
1 parent 7835512 commit 4522e85

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

README.markdown

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Configurable Options
3131
`headers` - An associative array of HTTP headers and values to be included in every request.
3232
`parameters` - An associative array of URL or body parameters to be included in every request.
3333
`curl_options` - cURL options to apply to every request; anything defined here: https://secure.php.net/manual/en/function.curl-setopt.php. These will override any automatically generated values.
34+
`build_indexed_queries` `(bool)` - `http_build_query` automatically adds an array index to repeated parameters which is not desirable on most systems. Use this option to enable the default behavior. Defaults to `FALSE`.
3435
`user_agent` - User agent string to use in requests.
3536
`base_url` - URL to use for the base of each request.
3637
`format` - Format string is appended to resource on request (extension), and used to determine which decoder to use on response; a request URL like "api.twitter.com/1.1/statuses/user_timeline.json" would be expected to return well-formed JSON.

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tcdent/php-restclient",
3-
"version": "0.1.6",
3+
"version": "0.1.7",
44
"description": "A generic REST API client for PHP",
55
"type": "library",
66
"keywords": ["REST", "api", "client", "curl", "JSON", "XML"],

restclient.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ class RestClientException extends Exception {}
1010

1111
class RestClient implements Iterator, ArrayAccess {
1212

13-
public $url;
1413
public $options;
1514
public $handle; // cURL resource handle.
1615

@@ -29,7 +28,8 @@ public function __construct($options=[]){
2928
'headers' => [],
3029
'parameters' => [],
3130
'curl_options' => [],
32-
'user_agent' => "PHP RestClient/0.1.6",
31+
'build_indexed_queries' => FALSE,
32+
'user_agent' => "PHP RestClient/0.1.7",
3333
'base_url' => NULL,
3434
'format' => NULL,
3535
'format_regex' => "/(\w+)\/(\w+)(;[.+])?/",
@@ -162,6 +162,13 @@ public function execute($url, $method='GET', $parameters=[], $headers=[]){
162162
if(is_array($parameters)){
163163
$parameters = array_merge($client->options['parameters'], $parameters);
164164
$parameters_string = http_build_query($parameters);
165+
166+
// http_build_query automatically adds an array index to repeated
167+
// parameters which is not desirable on most systems. This hack
168+
// reverts "key[0]=foo&key[1]=bar" to "key[]=foo&key[]=bar"
169+
if(!$client->options['build_indexed_queries'])
170+
$parameters_string = preg_replace(
171+
"/%5B[0-9]+%5D=/simU", "%5B%5D=", $parameters_string);
165172
}
166173
else
167174
$parameters_string = (string) $parameters;

test.php

+28-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function test_get(){
3535

3636
$api = new RestClient;
3737
$result = $api->get($TEST_SERVER_URL, [
38-
'foo' => ' bar', 'baz' => 1, 'bat[]' => ['foo', 'bar']
38+
'foo' => ' bar', 'baz' => 1, 'bat' => ['foo', 'bar']
3939
]);
4040

4141
$response_json = $result->decode_response();
@@ -52,7 +52,7 @@ public function test_post(){
5252

5353
$api = new RestClient;
5454
$result = $api->post($TEST_SERVER_URL, [
55-
'foo' => ' bar', 'baz' => 1, 'bat[]' => ['foo', 'bar']
55+
'foo' => ' bar', 'baz' => 1, 'bat' => ['foo', 'bar']
5656
]);
5757

5858
$response_json = $result->decode_response();
@@ -183,6 +183,32 @@ public function test_status_only_response(){
183183
$this->assertEquals((object) [], $api->headers);
184184
$this->assertEquals("", $api->response);
185185
}
186+
187+
public function test_build_indexed_queries(){
188+
global $TEST_SERVER_URL;
189+
190+
$api = new RestClient(['build_indexed_queries' => TRUE]);
191+
$result = $api->get($TEST_SERVER_URL, [
192+
'foo' => ' bar', 'baz' => 1, 'bat' => ['foo', 'bar', 'baz[12]']
193+
]);
194+
195+
$response_json = $result->decode_response();
196+
$this->assertEquals("foo=+bar&baz=1&bat%5B0%5D=foo&bat%5B1%5D=bar&bat%5B2%5D=baz%5B12%5D",
197+
$response_json->SERVER->QUERY_STRING);
198+
}
199+
200+
public function test_build_non_indexed_queries(){
201+
global $TEST_SERVER_URL;
202+
203+
$api = new RestClient;
204+
$result = $api->get($TEST_SERVER_URL, [
205+
'foo' => ' bar', 'baz' => 1, 'bat' => ['foo', 'bar', 'baz[12]']
206+
]);
207+
208+
$response_json = $result->decode_response();
209+
$this->assertEquals("foo=+bar&baz=1&bat%5B%5D=foo&bat%5B%5D=bar&bat%5B%5D=baz%5B12%5D",
210+
$response_json->SERVER->QUERY_STRING);
211+
}
186212
}
187213

188214

0 commit comments

Comments
 (0)