php-extended/php-api-fr-insee-ban-object

实现 php-extended/php-api-fr-insee-ban-interface 库的库


README

实现 php-extended/php-api-fr-insee-ban-interface 库的库。

该库使ApiFrInsee、Dgfip、OpenStreetMap贡献者、La Poste和Etalab的协作工作成果(称为BAN)作为对象可用于填充数据库。BAN可在以下URL获取:https://adresse.data.gouv.fr/

coverage build status

安装

此库的安装通过composer进行,并通过其自动加载器自动加载该库的所有类。

  • 他们的网站下载 composer.phar
  • 然后运行以下命令将此库作为依赖项安装
  • php composer.phar require php-extended/php-api-fr-insee-ban-object ^7

基本用法

此库提供了一个唯一的端点:\PhpExtended\Insee\InseeCogEndpoint,可以通过它检索所有数据。

  • 对于大多数用法,您可以使用以下代码

use PhpExtended\Insee\InseeBanEndpoint;
use PhpExtended\Endpoint\GzipHttpEndpoint;
use PhpExtended\Endpoint\HttpEndpoint;

/** $client \Psr\Http\Client\ClientInterface */

$endpoint = new ApiFrInseeBanEndpoint(new GzipHttpEndpoint(new HttpEndpoint($client)));

$lastDateTime = $endpoint->getLatestDate();
$departements = $endpoint->getDepartementIds($lastDateTime);

foreach($departements as $departement)
{
	$banLineIterator = $endpoint->getBanLinesIterator($lastDateTime, $departement);
	
	foreach($banLineIterator as $banLine)
	{
		/** @var $banLine \PhpExtended\Insee\InseeBanLine */
		// do something with the ban line representing :
		// - a commune with CodeInsee and NomCommune
		// - a group (street) with Id, NomVoie and NomComplementaire
		// - an address with Id, Numero, Suffixe, CodePostal
		// - a position with Id, X, Y, Lat, Lon
		// - and generic data like type of localisation,
		//     source and date of last update
	}
}

请注意,客户端必须遵循一系列关于通过X-Php-Download-File请求和响应头处理文件的规则,以便能够解压缩下载的文件而无需使用数GB的内存。它还必须返回一个X-Request-Uri头以获取完整的URI。

对于所需的最小HTTP客户端示例,请查看InseeBanEndpointTest类文件并找到使用的客户端。

但是,如果您有足够的内存,您可以绕过GzipHttpEndpoint以及所有X-Php-Download-FileX-Request-Uri的把戏。

许可证