【问题标题】:PHP and JSON Query to pull data from FreebasePHP 和 JSON 查询从 Freebase 中提取数据
【发布时间】:2011-12-24 00:45:32
【问题描述】:

我需要提取任何给定电影的制作公司和发行公司的信息,我正在使用 Freebase。碰巧我从未使用过 JSON,也不知道如何将查询与我的 PHP 文件集成。

我一直在阅读的教程对此一无所知,所以我一如既往地来这里获得我一直得到的帮助! :)

所以...我需要提取的信息是Freebase Query Editor

我拥有的 PHP 是这个(注释行是我确定错误的行和/或我遵循的教程中没有成功的行 here:

$moviename = "The Matrix";

// [{ "name": "The Matrix", "type": "/film/film", "production_companies": [{ "name": null }], "distributors": [{ "distributor": [{ "name": null }] }] }]
$simplequery = array('name'=>$moviename, 'type'=>"/film/film", 'production_companies'=>array('name'=>null));

//{"id":"/topic/en/philip_k_dick", "/film/writer/film":[]}
//$simplequery = array('id'=>$_POST["freebasewriter"], 'name'=>null, '/film/writer/film'=>array(array('name'=>null, 'id'=>null)));

$queryarray = array('q1'=>array('query'=>$simplequery));
$jsonquerystr = json_encode($queryarray);

#run the query
$apiendpoint = "http://sandbox.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); 

$jsonquerystr = urlencode(json_encode($queryarray) );

//print_r($resultarray);
//$produtoraarray = $resultarray["q1"]["result"]["production_companies"];
//print_r($produtoraarray);
//$produtorarray = $resultarray["q1"]["result"][];
//$freebaseserver = "http://sandbox.freebase.com";

这里有人可以帮我吗?提前致谢!

非常感谢,顺便说一句圣诞快乐:)

编辑 @Yanir Shahak 的回答很有帮助,现在我明白了:

Array ( [code] => /api/status/ok [q1] => Array ( [code] => /api/status/error [messages] => Array ( [0] => Array ( [code] => /api/status/error/mql/result [info] => Array ( [count] => 3 [result] => Array ( [0] => Array ( [name] => Warner Bros. Entertainment ) [1] => Array ( [name] => Village Roadshow Pictures ) [2] => Array ( [name] => Silver Pictures ) ) ) [message] => Unique query may have at most one result. Got 3 [path] => production_companies [query] => Array ( [name] => The Matrix [production_companies] => Array ( [error_inside] => . [name] => ) [type] => /film/film ) ) ) ) [status] => 200 OK [transaction_id] => cache;cache02.sandbox.sjc1:8101;2011-12-24T02:01:34Z;0004 ) 

现在...如何将数据从那里取出到我可以使用的数组中?

此代码不起作用,因为我得到未定义的索引

//print_r($resultarray);
//$produtoraarray = $resultarray["q1"]["result"]["production_companies"];
//print_r($produtoraarray);
//$produtorarray = $resultarray["q1"]["result"][];
//$freebaseserver = "http://sandbox.freebase.com";

【问题讨论】:

  • 我已经编辑了我的答案以匹配你的编辑。

标签: php json curl freebase mql


【解决方案1】:

您的查询返回错误,因为它缺少一些重要的细节。你写过:

$simplequery = array('name'=>$moviename, 
                     'type'=>"/film/film", 
                     'production_companies'=>array('name'=>null));

对应于以下 MQL 查询:

{
  "name": "The Matrix", 
  "type": "/film/film", 
  "production_companies": {
    "name": null
  }
}

这失败了,因为通常有不止一部电影有一个给定的名字,而且可能有不止一家制作公司参与其中。要在 MQL 中正确处理这些情况,您需要将 production_companies 属性以及完整的查询包装在数组中,以指示您期望查询的这些部分有多个结果。在 MQL 中看起来像这样:

[{
  "name": "The Matrix", 
  "type": "/film/film", 
  "production_companies": [{
    "name": null
  }]
}]

在你的代码中,它会转化为这样的东西:

$notsosimplequery = array(array('name'=>$moviename, 
                                'type'=>"/film/film",
                                'production_companies'=>array(array('name'=>null))));

您还应该考虑使用new MQL Read Service,它速度更快,并且对您每天可以进行的查询数量有更高的限制。

【讨论】:

  • 刚才我注意到我一个月前用你的答案解决了我的问题,这里没有接受!完毕!干杯
【解决方案2】:

你所要做的就是替换:

$jsonquerystr = json_encode( $queryarray );

与:

$jsonquerystr = urlencode( json_encode( $queryarray ) );

显然,在使用 cURL 将特殊字符发送到 API 服务器之前,您必须对 json 字符串中的特殊字符进行编码,因为 cURL 不会为您执行此操作...

当您返回结果时,它将采用 JSON(JavaScript 对象表示法)的形式,这是表示数据(属性和值)的另一种方式,类似于 XML。 您必须解码 json(请注意,当您发送查询时,您对 JSON 进行了编码)并将结果用作对象,如下所示:

$data = json_decode( $jsonresultstr );
foreach ( $data->q1->messages[ 0 ]->info->result as $company )
{
    echo( $company->name . "<br/>" );
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    相关资源
    最近更新 更多