mettle/big-huge-thesaurus-client

围绕 Big Huge Thesaurus API 的 PHP 客户端

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

This package is auto-updated.

Last update: 2024-09-04 14:59:40 UTC


README

Latest Version on Packagist Software License Total Downloads

PHP 封装 Big Huge Labs Thesaurus 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)。请参阅 许可证文件 以获取更多信息。