echo - PHP Parsing Freebase Query -


this basic question ever asked on site! new programming , new freebase - yet couldn't find answer need anywhere, here goes...

i using basic php query here in php file:

<?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']; ?> 

and have working displays result (in case name 'bob dylan') on website. question is, need pull down several pieces of info, date of birth, nationality, death,etc... cannot figure out how access, parse , display via echo $topic[?????????];.

how 1 figure out put here: echo $topic['????'];

i have result on website this:

name: bob dylan born: may 24, 1941 died: - nationality: american parents: ???, ??? children:  ???, ??? 

apologies extremely newb question not sure else turn.

thank you!!

. . . . . nicolas responding. followed website posted , tried instead:

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

but gives me following results on site: array(1) { ["array"]=> string(0) "" }

i hear sound of many palms hitting foreheads out there read this...like said greenhorn , appreciate help, thank you!

to answer 2nd part of question need understand how json data works , how code above parsing it.

if add /people/person/ethnicity query in 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() )); 

this query gets translated following json object:

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

that json object url encoded , added api request url this:

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:[]}] 

if open url in web browser, you'll see json object gets returned freebase api:

{   "result": [     {       "/people/person/gender": "male",        "/people/person/ethnicity": [         "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     }   ] } 

in code, data parsed php array objects , stored in $response variable this:

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

now if want loop on every topic returned freebase api, can in php this:

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

notice how use square braces tell iterate though each of objects contained within 'result' array entry in $response. query gave, there's 1 topic returned first , time through loop value of $topic is:

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 ) 

now, you're able access data within $topic using same square braces drill down 1 more level this:

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

by combining foreach loop used above can iterate on of ethnicity values this:

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

i hope gives better understanding of how navigate way through json data. understanding techniques i've described above, should able parse value out of freebase query.

if @ point need debug value of variable contains json data can print screen this:

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

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -