7.x common.inc drupal_http_build_query(array $query, $parent = '')

Parses an array into a valid, rawurlencoded query string.

This differs from http_build_query() as we need to rawurlencode() (instead of urlencode()) all query parameters.


$query: The query parameter array to be processed, e.g. $_GET.

$parent: Internal use only. Used to build the $query array key for nested items.

Return value

A rawurlencoded string which can be used as or appended to the URL query string.

See also


Related topics

8 calls to drupal_http_build_query()
CommonURLUnitTest::testDrupalHttpBuildQuery in modules/simpletest/tests/common.test
Test drupal_http_build_query().
drupal_current_script_url in includes/install.inc
Returns the URL of the current script, with modified query parameters.
image_style_url in modules/image/image.module
Returns the URL for an image derivative given a style and image path.
install_redirect_url in includes/install.core.inc
Returns the URL that should be redirected to during an installation request.
MenuTestCase::assertMenuLink in modules/menu/menu.test
Fetch the menu item from the database and compare it to the specified array.

... See full list


includes/common.inc, line 486
Common functions that many Drupal modules will need to reference.


function drupal_http_build_query(array $query, $parent = '') {
  $params = array();
  foreach ($query as $key => $value) {
    $key = $parent ? $parent . rawurlencode('[' . $key . ']') : rawurlencode($key);

    // Recurse into children.
    if (is_array($value)) {
      $params[] = drupal_http_build_query($value, $key);
    elseif (!isset($value)) {
      $params[] = $key;
    else {

      // For better readability of paths in query strings, we decode slashes.
      $params[] = $key . '=' . str_replace('%2F', '/', rawurlencode($value));
  return implode('&', $params);


Danny_Joris’s picture

If you are upgrading from D6 to D7 and used drupal_query_string_encode() in D6 to create encoded strings to populate the url() 'query' array, mind that in D7 the url() function calls drupal_http_build_query() to encode raw query array's.

So in D7 you don't have to encode your query strings with drupal_http_build_query() manually before adding them to url() .

This also counts for the l() function as it calls url().

qqboy’s picture

$query = drupal_http_build_query(array('a'=>'b'));
$query = 'a=b'

qqboy’s picture

$query = drupal_http_build_query(array('a'=>'b'),'ben');
$query = "ben[a]=b"

sanduhrs’s picture

ianwesty’s picture

It appears the change in drupal_http_build_query() on line 490 has caused an issue with Facetapi and Pretty Paths.
$key = $parent ? $parent . rawurlencode('[' . $key . ']') : rawurlencode($key);

Changing it back to the previous version appears to fix the issue.
$key = ($parent ? $parent . '[' . rawurlencode($key) . ']' : rawurlencode($key));