PHP code example of fize / provider-region
1. Go to this page and download the library: Download fize/provider-region 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/ */
fize / provider-region example snippets
ize\Provider\Region\RegionFactory;
use Fize\Provider\Region\RegionHandler;
$handler = (new RegionFactory())->create(RegionHandler::NBS);
// 查询省列表
foreach ($handler->getProvinces() as $province) {
echo $province->id . ': ' . $province->name . PHP_EOL;
}
// 根据父级编码查询下一级
$cities = $handler->getCitys(35); // 福建省
$countys = $handler->getCountys(3505); // 泉州市
$towns = $handler->getTowns(350521); // 惠安县
$villages = $handler->getVillages(350521111); // 东桥镇
// 根据编码生成完整名称
echo $handler->getFullName(350521111219) . PHP_EOL;
// 福建省泉州市惠安县东桥镇莲塘村委会
echo $handler->getFullName(110109, '-', 1) . PHP_EOL;
// 北京市-门头沟区
use Fize\Provider\Region\Handler\MCA;
$handler = new MCA();
$name = $handler->getFullName(110109, '-');
// 北京市-市辖区-门头沟区
$handler->getFullName(350581, '-', 0); // 福建省-泉州市-石狮市
$handler->getFullName(350581, '-', 2); // 福建省-石狮市
text
src/
├── Handler/ # Local、MCA、NBS 处理器
├── RegionFactory.php # 处理器工厂
├── RegionHandler.php # 处理器常量
├── RegionHandlerInterface.php
└── RegionItem.php # 行政区划项
database/ # 随项目发布的 SQLite 快照
tests/Handler/ # 处理器测试
tests/Updater/ # 数据更新脚本
bash
vendor/bin/phpunit tests/Handler/TestNBS.php
vendor/bin/phpunit tests/Handler/TestMCA.php
vendor/bin/phpunit tests/TestRegionFactory.php