Freebase上的MQL中的多个查询

时间:2012-05-02 12:35:33

标签: php freebase 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>";
}

2 个答案:

答案 0 :(得分:2)

你做的一切都是正确的。如果不是,那么您将收到JSON结果中的错误消息。

我认为发生的事情是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/- /膜/写入器/膜)。

多个查询

你没有准确地说明你要对MID数组做什么,但是发出多个查询就像在$ queryarray中添加q2,q3等一样简单。答案将在同一个结构中返回 - 您可以像拔出q1数据一样将它们拉出来。如果你打印出你的jsonquerystr和jsonresultstr,你会看到发生了什么。

答案 1 :(得分:0)

修改了一下以包含对问题的回答,因为这有助于我对每个人进行投票,只是认为我会提供更多“完整”答案,因为它是:

$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>";
    }
}