2

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

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

所以...我需要提取的信息是这个Freebase 查询编辑器

我拥有的 PHP 是这样的(注释行是我确定错误的行和/或我遵循的教程中的行,但此处没有成功:

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

2 回答 2

5

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

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

您还应该考虑使用新的 MQL 读取服务,该服务速度更快,并且对您每天可以进行的查询数量有更高的限制。

于 2011-12-26T07:16:43.410 回答
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/>" );
}
于 2011-12-24T01:35:29.357 回答