adding a better query string parser that can handle arrays

This commit is contained in:
Ahmad Nassri
2014-12-17 23:57:06 -05:00
parent dd9edca044
commit 8ee4ba0753
2 changed files with 16 additions and 12 deletions

View File

@@ -188,7 +188,7 @@ class Request
if ($method != Method::GET) { if ($method != Method::GET) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if (is_array($body) || $body instanceof Traversable) { if (is_array($body) || $body instanceof \Traversable) {
self::buildHTTPCurlQuery($body, $postBody); self::buildHTTPCurlQuery($body, $postBody);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postBody); curl_setopt($ch, CURLOPT_POSTFIELDS, $postBody);
} else { } else {
@@ -239,17 +239,15 @@ class Request
return new Response($httpCode, $body, $header); return new Response($httpCode, $body, $header);
} }
private static function getArrayFromQuerystring($querystring) private static function getArrayFromQuerystring($query)
{ {
$pairs = explode('&', $querystring); $query = preg_replace_callback('/(?:^|(?<=&))[^=[]+/', function($match) {
$vars = array(); return bin2hex(urldecode($match[0]));
foreach ($pairs as $pair) { }, $query);
$nv = explode('=', $pair, 2);
$name = $nv[0]; parse_str($query, $values);
$value = $nv[1];
$vars[$name] = $value; return array_combine(array_map('hex2bin', array_keys($values)), $values);
}
return $vars;
} }
/** /**
@@ -268,7 +266,7 @@ class Request
$query = (isset($url_parsed['query']) ? $url_parsed['query'] : null); $query = (isset($url_parsed['query']) ? $url_parsed['query'] : null);
if ($query != null) { if ($query != null) {
$query = '?' . http_build_query(self::getArrayFromQuerystring($url_parsed['query'])); $query = '?' . http_build_query(self::getArrayFromQuerystring($query));
} }
if ($port && $port[0] != ':') { if ($port && $port[0] != ':') {

View File

@@ -29,6 +29,12 @@ class Response
} }
} }
/**
* if PECL_HTTP is not available use a fall back function
*
* thanks to ricardovermeltfoort@gmail.com
* http://php.net/manual/en/function.http-parse-headers.php#112986
*/
if (!function_exists('http_parse_headers')) { if (!function_exists('http_parse_headers')) {
function http_parse_headers($raw_headers) { function http_parse_headers($raw_headers) {
$headers = array(); $headers = array();