mettleworks/big-huge-thesaurus-client

Big Huge Thesaurus API 的 PHP 客户端

v1.2.0 2022-02-04 09:15 UTC

This package is not auto-updated.

Last update: 2024-09-27 09:25:31 UTC


README

Latest Version on Packagist Software License Total Downloads

PHP 包装了 Big Huge Labs 词典 API

安装

通过 Composer

$ composer require mettle/big-huge-thesaurus-client

用法

$API_KEY = 'Get an api key @ https://words.bighugelabs.com/api.php';

$client = new Mettleworks\BigHugeThesaurusClient\BigHugeThesaurusClient($API_KEY);
$response = $client->lookup('love');

响应对象是 ThesaurusResponse 的一个实例,这是一个包装原始响应并添加一些便利方法的对象。您可以可选地为每个方法传递一个参数,以指示您是否只想获取名词(noun)、动词(verb)、形容词(adjective)或副词(adverb),否则将返回所有结果组合。方法的签名如下

/**
 * Get the synonyms from the response
 */
$response->getSynonyms($type = null): array

/**
 * Get the antonyms from the response
 */
$response->getAntonyms($type = null): array

/**
 * Get the similar terms from the response
 */
$response->getSimilarTerms($type = null): array

/**
 * Get related terms from the response
 */
$response->getRelatedTerms($type = null): array

/**
 * Cast response to array (returns the original response)
 */ 
$response->toArray(): array

/**
 * Cast response to JSON
 */
$response->toJson()

当发生错误时,客户端会抛出特定的异常。这些错误在 Big Huge Thesaurus Api 页面 上有文档说明。

try {
    $response = $client->lookup('love');
} catch (NotFoundException $ex) {
    // Not Found
} catch (UsageExceededException $ex) {
    // Usage exceeded
} catch (InactiveKeyException $ex) {
    // Key not active
} catch (MissingWordsException $ex) {
    // No words provided
} catch (NotWhitelistedException $ex) {
    // IP address blocked
}

此外,如果发生列表中未列出的错误,将抛出原始 Guzzle 异常。

测试

$ composer test

贡献

有关详细信息,请参阅 CONTRIBUTING

安全

如果您发现任何与安全相关的问题,请通过电子邮件 maurizio@mettle.io 而不是使用问题跟踪器来联系。

鸣谢

许可证

MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件