Freebase MID数组,并将多个查询发送到Freebase [英] Array of Freebase MIDs and sending multiple queries to freebase

查看:287
本文介绍了Freebase MID数组,并将多个查询发送到Freebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想遍历MySQL结果,它是Freebase MID的数组,并且我希望输出是Freebase文章的名称!

I want to loop through the MySQL results which is an array of Freebase MIDs and I want the output to be the names of the Freebase article!

这是我的代码:

$query = "SELECT `mid` FROM `items`";
$result = mysql_query($query);
$count = 1;
while ($row = mysql_fetch_array($result)) {
   $mid = $row['mid'];
   $simple_query = array('name'=> null, 'mid'=>$mid);
   $q_array = array('q'.$count=>array('query'=>$simple_query));
   array_push ($query_array, $q_array );
   $count++;
}

$jsonquerystr = json_encode($query_array);
$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);
$resultarray = json_decode($jsonresultstr, true); #true:give us the json struct as an array

foreach($resultarray as $name){
    echo "$name<br>";
}

错误:

{代码":"/api/status/error",消息":[{代码":"/api/status/error",消息":列表对象没有属性'get '}],"状态:" 500内部错误," transaction_id:"缓存; cache04.p01.sjc1:8101; 2012-05-14T07:31:39Z; 0079}

{ "code": "/api/status/error", "messages": [ { "code": "/api/status/error", "message": "'list' object has no attribute 'get'" } ], "status": "500 Internal Error", "transaction_id": "cache;cache04.p01.sjc1:8101;2012-05-14T07:31:39Z;0079" }

推荐答案

您的$query_array应该只是名称查询的关联数组,而不是关联数组.所以代替这个:

Your $query_array should just be an associative array of queries by name rather than an array of associative arrays. So instead of this:

$q_array = array('q'.$count=>array('query'=>$simple_query));
array_push ($query_array, $q_array );

..它应该看起来像这样:

..it should look something like this:

$q_array = array('query'=>$simple_query);
$query_array['q'.$count] = $q_array;

但是,该代码使用的是即将被弃用的旧版Freebase API.在新API 中执行此操作的更好方法是将其全部构造为一个这样的查询:

However, that code is using the old Freebase API which is about to be deprecated. A better way to do this in the new API would be to structure it all as one query like this:

[{
  "mid": null,
  "name": null,
  "topics:mid|=":[
    "/m/045c7b",
    "/m/0d6lp",
    "/m/021ympy",
    ...
    ]
}]

这使您可以传递一组MID,并为每个ID取回一个名称.从新的API中获取该URL的网址应类似于以下内容:

This lets you pass in an array of MIDs and get back a name for each one. The URL to fetch this from the new API should look something like this:

https://www.googleapis.com/freebase/v1/mqlread/?query=[{...}]

这篇关于Freebase MID数组,并将多个查询发送到Freebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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