Freebase上的MQL中的多个查询 [英] Multiple Queries in MQL on Freebase

查看:96
本文介绍了Freebase上的MQL中的多个查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to get a list of results from Freebase. I have an array of MIDs. Can someone explain how I would structure the query and pass it to the API in PHP?

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

I'm new to MQL - I can't even seem to get the example to work:

$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结果中返回错误消息.

You're doing everything right. If you weren't, you'd be getting back error messages in your JSON result.

我认为所发生的是,菲利普·迪克(Philip K. Dick)的数据已更新,以确认他不是电影的作者",而是电影_故事_贡献者". (毕竟,他实际上并没有写任何剧本.)

I think what's happened is that the data on Philip K. Dick has been updated to identify him not as the "writer" of films, but as a "film_story_contributor". (He didn't, after all, actually write any of the screenplays.)

从以下位置更改您的simplequery:

Change your simplequery from:

$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/zh/philip_k_dick)上,单击底部的编辑和显示详细信息"按钮.

You actually can use the Freebase website to drill down into topics to dig up this information, but it's not that easy to find. On the basic Philip K. Dick page (http://www.freebase.com/view/en/philip_k_dick), click the "Edit and Show details" button at the bottom.

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

The "edit" page (http://www.freebase.com/edit/topic/en/philip_k_dick) shows the Types associated with this topic. The list includes "Film story contributor" but not "writer". Within the Film story contributor block on this page, there's a "detail view" link (http://www.freebase.com/view/en/philip_k_dick/-/film/film_story_contributor/film_story_credits). This is, essentially, what you're trying to replicate with your PHP code.

对实际电影制片人(例如,史蒂夫·马丁)的类似钻取,将您带到名为/film/writer/film(http://www.freebase.com/view/zh/steve_martin/-/film/writer/film).

A similar drill-down on an actual film writer (e.g., Steve Martin), gets you to a property called /film/writer/film (http://www.freebase.com/view/en/steve_martin/-/film/writer/film).

多个查询

您没有确切说明要使用MID数组进行的操作,但是触发多个查询就像在$ queryarray内添加q2,q3等一样简单.答案将返回相同的结构内-您可以像拉出q1数据一样拉出它们.如果您打印出jsonquerystr和jsonresultstr,您将看到发生了什么事.

You don't say exactly what you're trying to do with an array of MIDs, but firing multiple queries is as simple as adding a q2, q3, etc., all inside the $queryarray. The answers will come back inside the same structure - you can pull them out just like you pull out the q1 data. If you print out your jsonquerystr and jsonresultstr you'll see what's going on.

这篇关于Freebase上的MQL中的多个查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆