faicchia / laravel-nationalize

nationalize.io 的服务包装器

1.0.0 2022-07-20 22:40 UTC

This package is auto-updated.

Last update: 2024-09-21 03:48:38 UTC


README

Packagist Tests Style License

nationalize.io 的服务包装器 nationalize.io

安装

composer require faicchia/laravel-nationalize

该包将自动注册自身。

配置

如果您想编辑包配置,可以运行以下命令将其发布到您的 config/ 文件夹

php artisan vendor:publish --provider="Faicchia\Nationalize\NationalizeServiceProvider"

环境

如果您购买了API密钥,请将以下行添加到您的 .env 文件中

NATIONALIZE_API_KEY=...

使用方法

单个名称

$response = Nationalize::name('Michael')->get()

print $response->status // 200 - HTTP response code

print $response->limit // 1000 - The amount of names available in the current time window

print $response->remaining // 728 - The number of names left in the current time window

print $response->reset // 15281 - Seconds remaining until a new time window opens

print $response->error // null - Error string

print $response->result->name // Michael

print $response->result->countries 
//  [
//    "US" => 0.08986482266532715,
//    "AU" => 0.05976757527083082,
//    "NZ" => 0.04666974820852911
//  ]

多个名称

$response = Nationalize::name(['Michael', 'Kevin'])->get()
// or
$response = Nationalize::names(['Michael', 'Kevin'])->get()

foreach ($response->result as $prediction) {
    print $prediction->name
    print $prediction->countries 
}

错误

// e.g. Invalid API Key
$response = Nationalize::name('Michael')->get()

print $response->status // 401 
print $response->error // "Invalid API key"