2013-05-02 65 views
2

這將是本站有史以來最基本的問題!我是新來的編程和很新的遊離鹼 - 但我無法找到我需要的任何地方,所以這裏去答案...PHP解析Freebase查詢

我使用從here PHP基本查詢在我的PHP文件:

<?php 
// include('.freebase-api-key'); 
    $service_url = 'https://www.googleapis.com/freebase/v1/topic'; 
    $topic_id = '/en/bob_dylan'; 
    $params = array('key'=>'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'); 
    $url = $service_url . $topic_id . '?' . http_build_query($params); 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $topic = json_decode(curl_exec($ch), true); 
    curl_close($ch); 
    echo $topic['property']['/type/object/name']['values'][0]['value']; 
?> 

我有這個工作,以便它在我的網站上顯示結果(本例中只是名字'鮑勃迪倫')。我的問題是,我需要拉下幾條信息,比如出生日期,國籍,死亡等等......但我無法弄清楚如何通過echo $ topic [?]來訪問,解析和顯示它。 ?????] ;.

如何弄清楚在這裏放什麼:echo $ topic ['????'];

我想在我的網站上是這樣的結果:

Name: Bob Dylan 
Born: May 24, 1941 
Died: - 
Nationality: American 
Parents: ???, ??? 
Children: ???, ??? 

道歉極其福利局問題,只是不知道還有什麼地方轉彎。

謝謝!

。 。 。 。 。 感謝尼古拉斯的迴應。我跟着那個網站,你發佈並試用了此相反:

$service_url = 'https://www.googleapis.com/freebase/v1/topic'; 
    $topic_id = '/en/bob_dylan'; 
    $params = array('key'=>'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'); 
    $url = $service_url . $topic_id . '?' . http_build_query($params); 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $topic = json_decode(curl_exec($ch), true); 
    curl_close($ch); 
    parse_str($topic, $txArr); 
    var_dump($txArr); 

但是,讓我在我的網站下面的結果:陣列(1){[「陣列」] =>字符串(0)「」}

我聽到許多手掌打在額頭上的聲音,因爲他們讀到這個...就像我說我是一個新手,並感謝一些幫助,所以謝謝!

+0

看看這個:HTTP://計算器。 com/questions/2459865/how-do-i-parse-the-response-i-get-back-from-curl – nicolas 2013-05-02 22:11:49

回答

1

要回答您的問題的第二部分,您的確需要了解JSON數據的工作原理以及您的代碼如何解析它。

如果添加/人/人/族裔來查詢它看起來像這樣在PHP:

$query = array(array(
    'id' => '/m/02mjmr', 
    '/people/person/gender' => NULL, 
    '/people/person/date_of_birth' => NULL, 
    '/people/person/height_meters' => NULL, 
    '/people/person/ethnicity' => array() 
)); 

該查詢被轉換爲下面的JSON對象:

[{ 
    "id": "/m/02mjmr", 
    "/people/person/gender": null, 
    "/people/person/date_of_birth": null, 
    "/people/person/height_meters": null, 
    "/people/person/ethnicity": [] 
}] 

這JSON對象然後被URL編碼並添加到API請求URL中,如下所示:

https://www.googleapis.com/freebase/v1/mqlread?query=[{%22id%22:%22/m/02mjmr%22,%22/people/person/gender%22:null,%22/people/person/date_of_birth%22:null,%22/people/person/height_meters%22:null,%22/people/person/ethnicity%22:[]}] 

如果你在Web瀏覽器中打開該網址,你會看到得到由遊離鹼API返回的JSON對象:

​​

在代碼中,這個數據被解析回PHP數組對象,並存儲在$響應變量像這樣:

array(
    'result' => array(array(
    '/people/person/ethnicity' => array(
     'English American', 
     'Kenyan American', 
     'Irish American', 
     'Multiracial American' 
    ), 
    'id' => '/m/02mjmr', 
    '/people/person/date_of_birth' => '1961-08-04', 
    '/people/person/height_meters' => 1.85 
)) 
) 

現在如果你想遍歷一個從遊離鹼API返回的每個話題,你可以做,在PHP這樣的:

foreach ($response['result'] as $topic) { ... } 

注意我們是如何使用SQU用大括號告訴它迭代$ response中的數組入口'result'中包含的每個對象。因爲你給了查詢,只有一個話題回到所以第一次也是唯一一次循環$話題的值是:

array(
    '/people/person/ethnicity' => array(
    'English American', 
    'Kenyan American', 
    'Irish American', 
    'Multiracial American' 
), 
    'id' => '/m/02mjmr', 
    '/people/person/date_of_birth' => '1961-08-04', 
    '/people/person/height_meters' => 1.85 
) 

現在,你可以通過這些來訪問$主題中的數據同方括號向下鑽取更多的這樣的水平:

echo $topic['/people/person/gender']; 

通過這與我們使用上面我們可以遍歷所有的種族值這樣的foreach循環相結合:

foreach ($topic['/people/person/ethnicity'] as $ethnicity) { 
    echo $ethnicity; 
} 

我希望這可以讓您更好地理解如何瀏覽通過JSON數據的方式。通過理解我上面描述的技術,您應該能夠解析任何Freebase查詢中的任何值。

如果在任何時候你需要調試包含一些JSON數據變量的值,你可以將其打印到這樣的畫面:

echo json_encode($topic['/people/person/ethnicity']) 
+0

哇,肖恩,謝謝你!你只是幫了我很多,我真的很感謝你花時間寫給我的時間和解釋。你很親切。再次感謝!! – user83404 2013-05-09 01:35:56

+0

不客氣。我很高興你發現它有幫助。 – 2013-05-09 04:31:05

1

好了,我發現了一個新的例子在這裏:https://developers.google.com/freebase/v1/mql-overview和我有工作如下:

$query = array(array('id' => '/m/02mjmr', '/people/person/gender' => NULL, '/people/person/date_of_birth' => NULL, '/people/person/height_meters' => NULL)); 
     $service_url = 'https://www.googleapis.com/freebase/v1/mqlread'; 
     $params = array(
       'query' => json_encode($query), 
       'key' => 'AIzaSyAPLxxxxxxxxxxxxxxxxx8INnsk7b2oxgc' 
     ); 
     $url = $service_url . '?' . http_build_query($params); 
     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, $url); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     $response = json_decode(curl_exec($ch), true); 
     curl_close($ch); 
     foreach ($response['result'] as $topic) { 
       echo "Gender: "; echo $topic['/people/person/gender'] . '<br/>'; 
       echo "Born: "; echo $topic['/people/person/date_of_birth'] . '<br/>'; 
       echo "Height (meters): "; echo $topic['/people/person/height_meters'] . '<br/>'; 

而這回是什麼樣子:

Gender: Male 
Born: 1961-08-04 
Height (meters): 1.85 

這正是我正在尋找但最後要弄清楚的是如何查詢和顯示多個屬性。我不能在我的查詢中包含「/ people/person/ethnicity」,因爲它會返回多個響應並中斷我的代碼。

因此,如果有人可以提示如何使多響應查詢和顯示可能會膨脹。

感謝您的幫助!