6

I want to construct this url using http_build_query:

https://www.googleapis.com/freebase/v1/topic/m/0d6lp?filter=/common/topic/notable_for&filter=/common/topic/alias

Please note that the "filter=" parameter comes twice in the url, to specify two filters.

I tried to do it this way but having a problem:

$service_url = 'https://www.googleapis.com/freebase/v1/topic';
$mid = '/m/0d6lp';
$params = array('filter' => '/common/topic/notable_for', 'filter' =>   '/common/topic/alias');
$url = $service_url . $mid . '?' . http_build_query($params);

The problem is as 'filter' array key repeats twice, only the last parameter appears in the http_build_query. How do I construct the original url with two filters?

3
  • Possible duplicate of http_build_query with same name parameters Commented Apr 26, 2017 at 17:01
  • 1
    I believe you should build the original URL with two filters using http_build_query(), then encode the array of filter values using the encodeArray() function. The encodeArray() function will encode the array in such a way that the duplicate keys are preserved. Commented Nov 1, 2023 at 11:28
  • 1
    For Exmaple ` $params = array('filter' => array('/common/topic/notable_for', '/common/topic/alias')); $encoded_filters = encodeArray($params['filter']); ` I think this will help you. Thanks. Commented Nov 1, 2023 at 12:03

5 Answers 5

9

The problem here of course is that every key in a PHP array (hash) can only have one value. Intrinsically, a PHP hash is not a good representation of a querystring, because the query string has an order and has no constraints about the uniqueness of the keys.

To combat this, you'll need a special querystring builder that can handle duplicate keys:

class QueryString {
    private $parts = array();

    public function add($key, $value) {
        $this->parts[] = array(
            'key'   => $key,
            'value' => $value
        );
    }

    public function build($separator = '&', $equals = '=') {
        $queryString = array();

        foreach($this->parts as $part) {
            $queryString[] = urlencode($part['key']) . $equals . urlencode($part['value']);
        }

        return implode($separator, $queryString);
    }

    public function __toString() {
        return $this->build();
    }
}

Example usage (Codepad Demo):

$qs = new QueryString();
$qs->add('filter', '1');
$qs->add('filter', '2');
var_dump($qs->build()); // filter=1&filter=2
3
  • But when it will be accessed via $_GET['filter'] it will return 2 not 1 & 2. You have to use 2 different key here. Commented Jun 18, 2013 at 5:40
  • 2
    @YogeshSuthar That is because $_GET is a hash (and cannot accurately represent a querystring). When PHP parses the QS, it overwrites duplicates. If you examine a querystring with duplicate keys (with php://input for POST or $_SERVER['QUERY_STRING'] for GET) you can extract the two values. Also consider keys like filter[], which PHP aggregates into an array. Querystrings allow for keys to be set multiple times. Commented Jun 18, 2013 at 5:45
  • 1
    @YogeshSuthar Also examine how Java handles it it returns an array of values for each key. This SO question also confirms that it is an established (although not documented) standard to allow keys to be assigned multiple values. Commented Jun 18, 2013 at 5:50
2

I have extended @Bailey Parker's answer to handle the issue raised in the first comment to his answer. This code will create query string where one key has multiple values and both values will be preserved using []

class QueryString
{
        private $parts = array();

        public function add($key, $value) {
                if(empty($value))return;
                if(is_array($value))
                {
                        foreach ($value as $v)
                                $this->add($key,$v);
                }
                else
                {
                        $this->parts[$key][] = $value;
                }

        }

        public function build($separator = '&', $equals = '=') {
                $queryString = array();

                $parts = array();
                foreach($this->parts as $key=>$value)
                {
                        if(count($value) > 1)
                                $parts[$key] = $value;
                        else
                                $parts[$key] = $value[0];
                }
                $query = http_build_query($parts);
                return preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '[]=', $query);;

        }

        public function __toString() {
                return $this->build();
        }

}

this code can also accept an array of values for a particular key

$qs = new QueryString();
$qs->add('trialId', array('1','2'));
$qs->add('packageId', '12');
$qs->add('frequencyId', '4');
var_dump($qs->build());

This class will produce normal query string, with the key having multiple values added with [] sign, the output will be like this

?frequencyId=4&packageId=12&trialId[]=1&trialId[]=2
1

PHP Associative Arrays will override the key with later assignment.

Hence, you should tweak your array a little bit from

$service_url = 'https://www.googleapis.com/freebase/v1/topic';
$mid = '/m/0d6lp';
$params = array('filter' => '/common/topic/notable_for', 'filter' =>   '/common/topic/alias');
$url = $service_url . $mid . '?' . http_build_query($params);

to

$service_url = 'https://www.googleapis.com/freebase/v1/topic';
$mid = '/m/0d6lp';
$params = array('filter' => ['/common/topic/notable_for', '/common/topic/alias']);
$url = $service_url . $mid . '?' . http_build_query_duplicate_key($params);
// https://www.googleapis.com/freebase/v1/topic/m/0d6lp?filter=/common/topic/notable_for&filter=/common/topic/alias

Explanation: The change from

array('filter' => '/common/topic/notable_for', 'filter' =>   '/common/topic/alias')

to

array('filter' => ['/common/topic/notable_for', '/common/topic/alias'])

is to ensure that both values are stored by PHP. Since PHP will override array value pair with the latest assignment. So, by changing it as above value will make the filter to store an array value which later will store it as:

array('filter' => [0 => '/common/topic/notable_for', 1=> '/common/topic/alias'])

http_build_query_duplicate_key is defined as follows:

function http_build_query_duplicate_key( array $array ) {
  $special_keys = [];
  foreach($array as $key => $value) {
    if (is_array($value) AND count($value) > 0) {
      if (array_is_list($value)) {
        foreach($value as $value_b) {
          /** Check if nested array that requires further breakdown */
          if (is_array($value_b) AND !array_is_list($value_b)) {
            foreach($value_b as $key2 => $value2) {
              /** Adding a $key2 in the bracket is needed for nested array */
              $special_keys[] = http_build_query_duplicate_key([$key.'['.$key2.']' => $value2]);
            }
          } else {
          /** Else, just build http query as usual. */
            $special_keys[] = http_build_query([$key => $value_b]);
          }
        }
      } else {
        /** PHP http_build_query will add array index number which are not accepted. Hence, we need to break it down manually. */
        $special_keys[] =  http_build_query_duplicate_key([$key => [$value]]);
      }
      unset($array[ $key ]);
    }
  }

  $and_symbol = '';
  if (count($array) > 0) {
    $and_symbol = '&';
  }

  return implode('&', $special_keys) . $and_symbol . http_build_query($array);
}

This code would works even when there is nested array as it will recursively call the method until all nested array are solved.

Hope this helps

1
  • 1
    I think a little more documentation on what the code is doing would be good to include, and why your solution works
    – Zach
    Commented Nov 2, 2023 at 18:22
0
  1. You cannot store in accoc array two or more values with one index.
  2. Api does not take values like filter[]

So, I suggest you to create your own function to build that query.

2
  • The API does support multiple filters. You can run the url I have given in the browser and see that the two filters apply. It is mentioned in the API documentation too.
    – Ninja
    Commented Jun 18, 2013 at 6:27
  • @Ninja, removed from answer.
    – sectus
    Commented Jun 18, 2013 at 6:38
-1

Not the best solution, but I have a workaround:

$item = [
   'key1' => array('value1','value2'),
   'key2' => array('value3','value4')
];

$array=[
    'aaa' => 'aaa',
    'null' => 'null&'.preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', http_build_query($item,null,'&')),
    'zzz' => 'zzz'
];

echo urldecode(http_build_query($array));

Output:

aaa=aaa&null=null&key1=value1&key1=value2&key2=value3&key2=value4&zzz=zzz

WARNING: probably this won't work cause url will be:

aaa=aaa&null=null%26key1%3Dvalue1%26key1%3Dvalue2%26key2%3Dvalue3%26key2%3Dvalue4&zzz=zzz

EDIT: Other option will be preg the result before assign it to the url:

preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', http_build_query($item))

output:

key1=value1&key1=value2&key2=value3&key2=value4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.