faicchia / laravel-nationalize
nationalize.io 的服务包装器
1.0.0
2022-07-20 22:40 UTC
Requires
- php: ^8.0
- guzzlehttp/guzzle: ^7.0
- illuminate/support: ^9.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.9
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
README
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"