Skip to content

Commit 0fe1348

Browse files
battisSeth Battis
authored andcommitted
Allow http_build_query() override
This seems like the kind of thing that one should never want to do. Unless... you're dealing with a RESTful API that doesn't seem to understand URL parameters that contain numeric indices for the base array. [For example](smtech/canvaspest#3): Instructure Canvas is expecting URL parameters for arrays that do not include numerical indices: ``` http://canvas-instance.instructure.com/api/v1/courses/123/sections?include[]=students&include[]=enrollments ``` At the same time [`http_build_query()`](https://github.com/smtech/canvaspest/blob/9110d69f756ff49e8da687381eaee52a767c0389/CanvasPest.php#L108) is trying to build legal URL parameters, which require that at least the base URL be indexed, resulting in shenanigans like this: ``` http://canvas-instance.instructure.com/api/v1/courses/123/sections?include%5B0%5D=students&include%5B1%5D=enrollments ``` It appears, at least, that Canvas tolerates URL-encoded arrays, but not the indices, so a request like this would work, allowing the result of `http_build_query()` to be post-processed [as described here](http://php.net/manual/en/function.http-build-query.php#111819): ``` http://canvas-instance.instructure.com/api/v1/courses/123/sections?include%5B%5D=students&include%5B%5D=enrollments ```
1 parent fcb539e commit 0fe1348

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

Pest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ public function setupProxy($host, $port, $user = NULL, $pass = NULL)
127127
$this->curl_opts[CURLOPT_PROXYUSERPWD] = $user . ":" . $pass;
128128
}
129129
}
130+
131+
/**
132+
* Allow overriding of `http_build_query()` for idiosyncratic APIs
133+
* @param mixed $data
134+
* @return string
135+
**/
136+
protected function http_build_query($data) {
137+
return http_build_query($data);
138+
}
130139

131140
/**
132141
* Perform HTTP GET request
@@ -143,7 +152,7 @@ public function get($url, $data = array(), $headers=array())
143152
if ($pos !== false) {
144153
$url = substr($url, 0, $pos);
145154
}
146-
$url .= '?' . http_build_query($data);
155+
$url .= '?' . $this->http_build_query($data);
147156
}
148157

149158
$curl_opts = $this->curl_opts;
@@ -410,7 +419,7 @@ public function prepData($data)
410419
}
411420
}
412421

413-
return ($multipart) ? $data : http_build_query($data);
422+
return ($multipart) ? $data : $this->http_build_query($data);
414423
} else {
415424
return $data;
416425
}
@@ -452,7 +461,7 @@ public function put($url, $data, $headers = array())
452461
*/
453462
public function patch($url, $data, $headers = array())
454463
{
455-
$data = (is_array($data)) ? http_build_query($data) : $data;
464+
$data = (is_array($data)) ? $this->http_build_query($data) : $data;
456465

457466
$curl_opts = $this->curl_opts;
458467
$curl_opts[CURLOPT_CUSTOMREQUEST] = 'PATCH';

0 commit comments

Comments
 (0)