farzai/geonames

There is no license information available for the latest version (1.0.3) of this package.

This package provides a simple way to download Geonames data and format it for friendly use.

Fund package maintenance!
parsilver

1.0.3 2023-08-22 14:32 UTC

This package is auto-updated.

Last update: 2024-12-18 14:28:25 UTC


README

Latest Version on Packagist Tests codecov Total Downloads

This package provides a simple way to download Geonames data and format it for friendly use. (https://www.geonames.org/)

Requirements

PHP ^8.1
ext-curl
ext-json
ext-zip

Setup

You can install the package via Composer:

composer require farzai/geonames

Example

use Farzai\Geonames\Client;

// Create a new client
$client = new Client();

// Get all countries and return $resource
$resource = $client->getCountryInfo(); // \Farzai\Geonames\Resource\CollectionResource

// Now, you can access the data using the following methods:
$countries = $resource->asArray(); // Array
$json = $resource->asJson(); // String

// Or acccess data as entity
foreach ($resource->all() as $country) {
    /** @var \Farzai\Geonames\Entities\CountryEntity $country */
    echo $country->name;
}

Available methods

Get all countries

// GET: https://download.geonames.org/export/dump/countryInfo.txt
$resource = $client->getCountryInfo();

// $entity instanceof \Farzai\Geonames\Entities\CountryEntity
foreach ($resource->all() as $entity) {
    echo $entity->iso;
    // ...
}

Get languages

// GET: https://download.geonames.org/export/dump/iso-languagecodes.txt
$resource = $client->getLanguages();

// $entity instanceof \Farzai\Geonames\Entities\LanguageEntity
foreach ($resource->all() as $entity) {
    // ...
}

Geonames available resources

// GET: https://download.geonames.org/export/dump
$resource = $client->getGeonamesAvailable();

/** @var string[] $countryCodes */
$countryCodes = $resource->all();

Geonames by country code

// GET: https://download.geonames.org/export/dump/{countryCode}.zip
$resource = $client->getGeonamesByCountryCode('TH');

// $entity instanceof \Farzai\Geonames\Entities\GeonameEntity
foreach ($resource->all() as $entity) {
    // ...
}

Alternate names available resources

// GET: https://download.geonames.org/export/dump/alternatenames
$resource = $client->getAlternateNamesAvailable();

/** @var string[] $countryCodes */
$countryCodes = $resource->all();

Alternate names by country code

// GET: https://download.geonames.org/export/dump/alternatenames/{countryCode}.zip
$resource = $client->getAlternateNamesByCountryCode('TH');

// $entity instanceof \Farzai\Geonames\Entities\AlternateNameEntity
foreach ($resource->all() as $entity) {
    //
}

License

The MIT License (MIT). Please see License File for more information.