-->

在MQL多个查询上的游离碱(Multiple Queries in MQL on Freebase)

2019-09-16 23:49发布

我试图得到的游离碱结果列表。 我的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>";
}

Answer 1:

你做的一切权利。 如果你没有,你会在你的JSON结果回去了错误消息。

我觉得发生了什么事是,在菲利普·迪克的数据已经更新到无法辨认他作为电影的“作家”,而作为“film_story_contributor”。 (他没有,毕竟,居然写任何剧本。)

从更改simplequery:

$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());

实际上,你可以使用游离碱网站,深入到主题,以挖掘该信息,但它不是那么容易找到。 在基本菲利普K.页(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代码复制什么。

在实际的电影编剧类似的向下钻取(例如,史蒂夫·马丁),让你到一个名为/膜/编剧/薄膜性能(http://www.freebase.com/view/en/steve_martin/-/film/编剧/膜)。

多重查询

你不要说你想要什么与MID的数组做的,但发射多个查询,只需添加一个Q2,Q3,等等,所有的$ queryarray里面一样简单。 这些问题的答案会回来相同的结构里面 - 你可以将它们拉出来,就像你拉出第一季度的数据。 如果你要打印出你的jsonquerystr和jsonresultstr你会看到发生了什么事情。



Answer 2:

修改了一下,包括回答提出了质疑,因为这帮助了我,我已经upvoted每个,只是想我会提供更多的“compleat”的答案,因为它是:

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


文章来源: Multiple Queries in MQL on Freebase
标签: php freebase mql