我正在尝试从 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·迪克的数据已更新,将他确定为“电影故事贡献者”,而不是电影的“作家”。 (毕竟,他实际上并没有写任何剧本。)
将您的简单查询更改为:
$simplequery = array('id'=>'/topic/en/philip_k_dick', '/film/writer/film'=>array());
To:
$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/作家/电影)。
多重查询
您没有确切说明您要对 MID 数组执行什么操作,但触发多个查询就像添加 q2、q3 等一样简单,所有这些都在 $queryarray 内。答案将返回到相同的结构中 - 您可以像提取 q1 数据一样提取它们。如果你打印出 jsonquerystr 和 jsonresultstr 你就会看到发生了什么。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)