在PHP中,如何在主题api中过滤多种类型


In PHP, how to filter multiple types in topic api?

我知道过滤多个类型是这样工作的:

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

但是当我必须在PHP中写这个时,我不确定如何去做。

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

当我curl $url时,只有最后一个过滤器(/common/topic/alias)生效,显然是因为'filter'键在数组$params中出现两次,并且只采用最新键的值。

如何在PHP数组中构造以下url ?

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

我不太确定你想做什么。如果你只是想用多个过滤器来执行查询,你可以直接在url字符串中添加它们,像这样:

https://www.googleapis.com/freebase/v1/topic/m/0d6lp?key= API_KEY&amp美元;过滤器=/共同/主题/notable_for&过滤器=/共同/主题/别名

你不需要使用任何数组,只需要将url构造为一个普通的php字符串。如果你仍然想在数组中拥有参数,我建议你对数组中的所有元素进行foreach循环来构造字符串,然后使用该字符串作为curl的参数。