2011-12-24 47 views
2

我需要爲任何給定的電影提取製作公司和發行公司的信息,並且我正在使用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

回答

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

你也應該考慮使用new MQL Read Service這是更快,有多少更高的極限您可以每天查詢。

+0

剛纔我注意到我在一個月前使用了你的答案來解決我的問題,而沒有在這裏接受它!完成!乾杯 – 2012-01-31 23:08:02

2

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

$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/>"); 
}