40 lines
1 KiB
PHP
40 lines
1 KiB
PHP
|
<?php
|
||
|
$config = parse_ini_file(__DIR__ . '/../config/flashii.ini');
|
||
|
|
||
|
header('Content-Type: application/json; charset=utf-8');
|
||
|
|
||
|
$word = (string)filter_input(INPUT_GET, 'word');
|
||
|
if(empty($word))
|
||
|
die('{"error":"word"}');
|
||
|
|
||
|
$response = json_decode(file_get_contents(sprintf(
|
||
|
'https://api.wordnik.com/v4/word.json/%s/definitions?limit=5&includeRelated=false&sourceDictionaries=ahd-5%%2Cwordnet&useCanonical=true&includeTags=false&api_key=%s',
|
||
|
rawurlencode($word),
|
||
|
$config['wordnik-token']
|
||
|
)));
|
||
|
|
||
|
if(empty($response))
|
||
|
die('{"error":"response"}');
|
||
|
|
||
|
$defs = [];
|
||
|
|
||
|
foreach($response as $def) {
|
||
|
if(empty($def->text))
|
||
|
continue;
|
||
|
|
||
|
$defs[] = [
|
||
|
'word' => $def->word ?? '',
|
||
|
'pos' => $def->partOfSpeech ?? '',
|
||
|
'attr' => $def->attributionText ?? '',
|
||
|
'attr_url' => $def->attributionUrl ?? '',
|
||
|
'dict' => $def->sourceDictionary ?? '',
|
||
|
'text' => strtr($def->text, [
|
||
|
'<em>' => '[i]',
|
||
|
'</em>' => '[/i]',
|
||
|
]),
|
||
|
'url' => $def->wordnikUrl ?? '',
|
||
|
];
|
||
|
}
|
||
|
|
||
|
echo json_encode($defs);
|