自由基地上的 MQL 中的多个查询



我正在尝试从Freebase获取结果列表。我有一系列 MID。有人可以解释一下我将如何构建查询并将其传递给 PHP 中的 API 吗?

我是 MQL 的新手 - 我什至无法让示例工作:

$simplequery = array('id'=>'/topic/en/philip_k_dick', '/film/writer/film'=>array());
$jsonquerystr = json_encode($simplequery);
// The Freebase API requires a query envelope (which allows you to run multiple queries simultaneously) so we need to wrap our original, simplequery structure in two more arrays before we can pass it to the API:
$queryarray = array('q1'=>array('query'=>$simplequery));
$jsonquerystr = json_encode($queryarray);
// To send the JSON formatted MQL query to the Freebase API use cURL:
#run the query
$apiendpoint = "http://api.freebase.com/api/service/mqlread?queries";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$apiendpoint=$jsonquerystr");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$jsonresultstr = curl_exec($ch);
curl_close($ch);
// Decoding the JSON structure back into arrays is performed using json_decode as in:
$resultarray = json_decode($jsonresultstr, true); #true:give us the json struct as an array
// Iterating over the pieces of the resultarray containing films gives us the films Philip K. Dick wrote:
$filmarray = $resultarray["q1"]["result"]["/film/writer/film"];
foreach($filmarray as $film){
    print "$film<br>";
}

你做的一切都是对的。 如果不是,则会在 JSON 结果中返回错误消息。

我认为发生的事情是,菲利普·K·迪克(Philip K. Dick(的数据已经更新,将他确定为"film_story_contributor",而不是电影的"作家"。 (毕竟,他实际上并没有写过任何剧本。

将简单查询从:

$simplequery = array('id'=>'/topic/en/philip_k_dick', '/film/writer/film'=>array());

自:

$simplequery = array('id'=>'/topic/en/philip_k_dick', '/film/film_story_contributor/film_story_credits'=>array());

您实际上可以使用Freebase网站向下钻取主题以挖掘此信息,但是要找到它并不容易。 在基本的 Philip K. Dick 页面 (http://www.freebase.com/view/en/philip_k_dick( 上,单击底部的"编辑和显示详细信息"按钮。

"编辑"页 (http://www.freebase.com/edit/topic/en/philip_k_dick( 显示与本主题关联的类型。 该列表包括"电影故事贡献者",但不包括"作家"。 在此页面上的电影故事贡献者块中,有一个"详细信息视图"链接 (http://www.freebase.com/view/en/philip_k_dick/-/film/film_story_contributor/film_story_credits(。 从本质上讲,这就是您尝试用PHP代码复制的内容。

对实际电影编剧(例如,史蒂夫·马丁(进行类似的向下钻取,会让你找到一个名为/film/writer/film(http://www.freebase.com/view/en/steve_martin/-/film/writer/film(的属性。

多个查询

你没有确切地说出你想用一个 MID 数组做什么,但触发多个查询就像在$queryarray中添加 q2、q3 等一样简单。 答案将回到相同的结构中 - 您可以像提取 q1 数据一样提取它们。 如果你打印出你的jsonquerystr和jsonresultstr,你会看到发生了什么。

修改了一点以包含问题的答案,因为这对我有帮助,我已经对每个都投了赞成票,只是想我会提供一个更"完整"的答案,因为它是:

$simplequery = array('id'=>'/topic/en/philip_k_dick', '/film/film_story_contributor/film_story_credits'=>array());
$jsonquerystr = json_encode($simplequery);
// The Freebase API requires a query envelope (which allows you to run multiple queries simultaneously) so we need to wrap our original, simplequery structure in two more arrays before we can pass it to the API:
$queryarray = array('q1'=>array('query'=>$simplequery));
$jsonquerystr = json_encode($queryarray);
// To send the JSON formatted MQL query to the Freebase API use cURL:
#run the query
$apiendpoint = "http://api.freebase.com/api/service/mqlread?queries";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$apiendpoint=$jsonquerystr");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$jsonresultstr = curl_exec($ch);
curl_close($ch);
// Decoding the JSON structure back into arrays is performed using json_decode as in:
$resultarray = json_decode($jsonresultstr, true); #true:give us the json struct as an associative array
// Iterating over the pieces of the resultarray containing films gives us the films Philip K. Dick wrote:
if($resultarray['code'] == '/api/status/ok'){
    $films = $resultarray['q1']['result']['/film/film_story_contributor/film_story_credits'];
    foreach ($films as $film){
        print "$film</br>";
    }
}

最新更新