Skip to content

Instantly share code, notes, and snippets.

@bitsmanent
Last active November 10, 2018 20:42
Show Gist options
  • Save bitsmanent/5205b96ac59da42b39a1 to your computer and use it in GitHub Desktop.
Save bitsmanent/5205b96ac59da42b39a1 to your computer and use it in GitHub Desktop.
Single function Freebase SDK
function freebase($query, $lang = 'it,en', $limit = 5) {
$isid = (strpos($query, '/m/') !== false);
if($isid)
$limit = 1;
$q = array(
'key' => 'PUT_YOUR_KEY_HERE',
'query' => $query,
'output' => '(name description notable)',
'lang' => $lang,
'limit' => $limit
);
$service = 'https://www.googleapis.com/freebase/v1/search';
$uri = $service.'?'.http_build_query($q);
$res = json_decode(file_get_contents($uri), true);
/* XXX check for errors (also HTTP errors) */
if(isset($res['error']))
return false;
if(!count($res['result']))
return array();
if($isid)
return $res['result'][0];
return $res['result'];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment