/** * Looks up a text in the dictionary * * @param string $text * @return array|bool * @throws DictionaryException * @throws ForbiddenException * @static */ public static function lookup($text) { return \Yandex\Dictionary\DictionaryClient::lookup($text); }
use Yandex\Common\Exception\ForbiddenException; use Yandex\Dictionary\Exception\DictionaryException; $errorMessage = false; // Is auth if (isset($_COOKIE['yaAccessToken']) && isset($_COOKIE['yaClientId'])) { if (!isset($settings["dictionary"]["key"]) || !$settings["dictionary"]["key"]) { throw new DictionaryException('Empty dictionary key. Get key from https://tech.yandex.ru/keys/get/?service=dict'); } $dictionaryClient = new DictionaryClient($settings["dictionary"]["key"]); if (isset($_POST['word']) && $_POST['word'] && isset($_POST['language']) && $_POST['language']) { $translation = explode('-', $_POST['language']); if (count($translation) === 2) { $from = $translation[0]; $to = $translation[1]; $dictionaryClient->setTranslateFrom($from)->setTranslateTo($to); $result = $dictionaryClient->lookup($_POST['word']); if ($result) { /** @var \Yandex\Dictionary\DictionaryDefinition $dictionaryDefinition */ $dictionaryDefinition = $result[0]; $dictionaryTranslation = $dictionaryDefinition->getTranslations(); /** @var \Yandex\Dictionary\DictionaryTranslation $dictionaryTranslation */ $dictionaryTranslation = $dictionaryDefinition->getTranslations()[0]; $word = $dictionaryTranslation->getText(); } } } try { $languages = $dictionaryClient->getLanguages(); } catch (ForbiddenException $ex) { $errorMessage = $ex->getMessage(); $errorMessage .= '<p>Возможно, у приложения нет прав на доступ к ресурсу. Попробуйте ' . '<a href="' . rtrim(str_replace($_SERVER['DOCUMENT_ROOT'], '', __DIR__), "/") . "/../OAuth/" . '">авторизироваться</a> и повторить.</p>';