Last active
November 10, 2018 20:42
-
-
Save bitsmanent/5205b96ac59da42b39a1 to your computer and use it in GitHub Desktop.
Single function Freebase SDK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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