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