faicchia/laravel-nationalize

A service wrapper around nationalize.io

1.0.0 2022-07-20 22:40 UTC

This package is auto-updated.

Last update: 2024-10-21 04:03:04 UTC


README

Packagist Tests Style License

A service wrapper around nationalize.io

Installation

composer require faicchia/laravel-nationalize

The package will automatically register itself.

Configuration

If you wish to edit the package configuration, you can run the following command to publish it into your config/ folder:

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

Environment

If you purchased an API Key, add the following line to your .env file

NATIONALIZE_API_KEY=...

Usage

Single name

$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
//  ]

Multiple names

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

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

Error

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

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