smart/geo

Smart Geo 是将来自开源数据提供商的数据库编译成易于使用的 PHP 对象。

0.4.0 2016-09-25 02:33 UTC

This package is not auto-updated.

Last update: 2024-09-11 14:48:13 UTC


README

Smart Geo

Build Status Latest Stable Version Total Downloads License

Smart Geo 是将来自开源数据提供商的数据库编译成易于使用的 PHP 对象。

  1. 特性
  2. 来源
  3. 需求
  4. 安装
  5. 国家
  6. 地区

特性

  • 多语言支持(目前仅支持英语、法语和德语)。
  • 国家数据库
  • 地区数据库(目前仅支持加拿大和美国)。
  • IP 地理位置信息(使用 MaxMind)

来源

需求

Smart Geo 不需要数据库,而是使用 JSON 文件。

此库使用 PHP 5.6+。

安装

您需要通过 composer 安装 Smart Geo 库。为此,请将以下行添加到您的 composer.json 文件中。

{
    "require": {
       "smart/geo": "dev-master"
    }
}

要下载或更新当前数据,请使用以下命令。

php vendor/bin/geo data:update

国家

世界上所有国家的数据库。

属性

  • 名称
  • 简码(Alpha-2 码)
  • 代码(Alpha-3 码)
  • 纬度
  • 经度
  • 边界框
  • 货币
  • 大陆
  • 人口
  • 面积
  • 首都
  • 时区

示例

获取所有国家的列表。

$countryCollection = (new Geo)->getCountryRepository()->findAll();

获取英语国家名称。

foreach ($countryCollection as $country) {
   echo $country->getNames()->get('en');
}

按英语国家名称排序。

$countryCollection->orderByName();

地区

美国所有州、联邦区和领地的数据库,加拿大各省和领地的数据库。

属性

  • 名称
  • 代码(Alpha-2 码)
  • 国家
  • 类型
  • 纬度
  • 经度
  • 边界框

示例

获取美国所有地区的列表。

$geo = new Geo;
$country = $geo->getCountryRepository()->findByCode('US');
$regionCollection = (new Geo)->getRegionRepository()->findByCountry($country);

获取地区名称和类型(英语)。

foreach ($regionCollection as $region) {
   echo $region->getNames()->get('en') . " is a " . 
       $region->getType()::class . " of the " . 
       $country->getNames()->get('en);
}