Created
September 23, 2014 15:34
-
-
Save inxilpro/f2d23c3ca388571226e2 to your computer and use it in GitHub Desktop.
http_build_query polyfill
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| if (!defined('PHP_QUERY_RFC1738')) { | |
| define('PHP_QUERY_RFC1738', 1); | |
| } | |
| if (!defined('PHP_QUERY_RFC3986')) { | |
| define('PHP_QUERY_RFC3986', 2); | |
| } | |
| if (!function_exists('http_build_query')) { | |
| function http_build_query($query_data, $numeric_prefix = null, $arg_separator = '&', $enc_type = PHP_QUERY_RFC1738, $recursive_prefix = null) { | |
| $query = ''; | |
| // Check for arg_separator.output INI setting | |
| $arg_separator_ini = ini_get('arg_separator.output'); | |
| if ('&' == $arg_separator && $arg_separator_ini) { | |
| $arg_separator = $arg_separator_ini; | |
| } | |
| // Loop thru query data | |
| foreach ($query_data as $key => $value) { | |
| // Handle numeric_prefix | |
| if (!$recursive_prefix && $numeric_prefix && is_int($key)) { | |
| $key = $numeric_prefix . $key; | |
| } | |
| // Handle recursive sub-arrays | |
| if ($recursive_prefix) { | |
| $key = $recursive_prefix . http_build_query_polyfill_encode('[' . $key . ']', $enc_type); | |
| } | |
| if (is_array($value)) { | |
| // Run recursively if necessary | |
| $query .= http_build_query($value, $numeric_prefix, $arg_separator, $enc_type, $key); | |
| } else { | |
| // Otherwise just encode | |
| $query .= $key . '=' . http_build_query_polyfill_encode($value, $enc_type) . $arg_separator; | |
| } | |
| } | |
| $query = rtrim($query, $arg_separator); | |
| return $query; | |
| } | |
| function http_build_query_polyfill_encode($value, $enc_type) { | |
| if (PHP_QUERY_RFC3986 == $enc_type) { | |
| return rawurlencode($value); | |
| } | |
| return urlencode($value); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment