farzai/geonames

本包的最新版本(1.0.3)没有可用的许可信息。

此包提供了一种简单的方法来下载Geonames数据,并对其进行格式化以方便使用。

资助包维护!
parsilver

1.0.3 2023-08-22 14:32 UTC

README

Latest Version on Packagist Tests codecov Total Downloads

此包提供了一种简单的方法来下载Geonames数据,并对其进行格式化以方便使用。 (https://www.geonames.org/)

需求

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

设置

您可以通过Composer安装此包

composer require farzai/geonames

示例

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;
}

可用方法

获取所有国家

// 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: https://download.geonames.org/export/dump/iso-languagecodes.txt
$resource = $client->getLanguages();

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

Geonames可用资源

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

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

按国家代码获取Geonames

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

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

备用名称可用资源

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

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

按国家代码获取备用名称

// 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) {
    //
}

许可

MIT许可(MIT)。请参阅许可文件以获取更多信息。