PHP和JSON查询从Freebase中提取数据

PHP和JSON查询从Freebase中提取数据

问题描述:

我需要为任何给定的电影提取制作公司和发行公司的信息,并且我正在使用Freebase。发生我从来没有使用JSON,也不知道如何将查询与我的PHP文件集成。PHP和JSON查询从Freebase中提取数据

我一直在阅读的教程并不完全清楚这一点,所以我总是来这里得到我总是得到的帮助! :)

所以...我需要拔出的信息是这样的Freebase Query Editor

,而PHP的我已经是这个(注释行是我肯定是错误的和/或线从线教程中,我跟着没有sucess 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沙哈克答案帮助,现在我得到这样的:

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"; 
+0

我编辑了我的答案来匹配你编辑。 – Yaniro 2011-12-26 07:28:58

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

$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这是更快,有多少更高的极限您可以每天查询。

+0

刚才我注意到我在一个月前使用了你的答案来解决我的问题,而没有在这里接受它!完成!干杯 – 2012-01-31 23:08:02

所有你需要做的就是更换:

$jsonquerystr = json_encode($queryarray); 

有了:

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

显然,你必须编码在使用cURL作为cURL将它们发送到API服务器之前,json字符串中的特殊字符不会为您执行。

当你得到你的结果时,它将以JSON(JavaScript对象表示法)的形式出现,这是表示数据(属性&值)的另一种方式,非常像XML。 你将不得不解码JSON(注意,当你发送的查询您编码的JSON),并使用结果作为像这样的对象:

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