PHP code example of farzai / geonames

1. Go to this page and download the library: Download farzai/geonames library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

farzai / geonames example snippets


use Farzai\Geonames\Geonames;

Geonames::countries();                          // Collection<Country>, 252, bundled
$th = Geonames::country('TH');                  // by ISO alpha-2, alpha-3 or numeric; ?Country
Geonames::countryOrFail('tha');                 // or NotFoundException
$th->name('th');                                // 'ไทย'; ->name('en'); ->name('ja') falls back to the default name
$th->nameTh;                                    // property or method, as in thailand-address
$th->currency();                                // ['code' => 'THB', 'name' => 'Baht']
$th->callingCode; $th->timeZones(); $th->postalCodeRegex();

Geonames::admin1('TH');                         // Collection<Admin1>, keyed by geoname_id and "TH.40"
Geonames::admin1Code('TH', '40')->iso31662();   // 'TH-10' (nullable, never a join key)
Geonames::admin2('TH', admin1: '50');
Geonames::cities('TH', minPopulation: 100_000); // Collection<City>
Geonames::city(1609350, 'TH');                  // by geoname_id, fetched countries only
Geonames::search('chiang', 'TH');               // names in any selected language, prefix first
Geonames::postalCodes('US', '90210');           // every row of a code, each with ->precision()
Geonames::nearest(13.75, 100.5, 'TH', limit: 5);// needs the SQLite file; each result has distance_km
Geonames::timeZones('TH');                      // ['Asia/Bangkok']

use Farzai\Geonames\Geonames;
use Farzai\Geonames\GeonamesRepository;

Geonames::swap(GeonamesRepository::fromArrays(
    countries: [['iso2' => 'XX', 'iso3' => 'XXX', 'iso_numeric' => '999', 'name' => 'Testland', 'names' => ['th' => 'เทสต์']]],
    cities: ['XX' => [['geoname_id' => 1, 'name' => 'Test City', 'names' => [], 'lat' => 1.0, 'lon' => 2.0, 'population' => 10]]],
));

Geonames::swap(null); // back to the real data

use Illuminate\Validation\Rule;

$request->validate([
    'country'     => ['ountry')],
    'city_id'     => ['

use Farzai\Geonames\Laravel\Casts\AsAdmin1;
use Farzai\Geonames\Laravel\Casts\AsCity;
use Farzai\Geonames\Laravel\Casts\AsCountry;

protected function casts(): array
{
    return ['country' => AsCountry::class, 'region' => AsAdmin1::class, 'city_id' => AsCity::class];
}

$customer->country = 'tha';                 // stores 'TH'
$customer->country->name('th');             // 'ไทย'
$customer->region = 'TH.50';                // stores the GeoNames code
$customer->city_id = 1153671;               // stores the geoname_id
sh
php artisan geonames:fetch TH US --datasets=admin1,admin2,cities
sh
php artisan vendor:publish --tag=geonames-migrations
php artisan migrate
php artisan geonames:seed TH US --datasets=countries,admin1,admin2,cities,postal_codes