devmobgroup / postcodes
此包已被放弃,不再维护。未建议替代包。
荷兰邮政编码数据获取的抽象层。
0.1.0
2018-10-04 13:39 UTC
Requires
- php: >=7.1
- ext-json: *
- adbario/php-dot-notation: ^2.1
- guzzlehttp/guzzle: ~6.0
- psr/http-message: ^1.0
Requires (Dev)
- phpunit/phpunit: ^7.3
Suggests
- devmobgroup/postcodes-api-postcode: Provider for Api-postcode.nl
- devmobgroup/postcodes-postcode-api-nu: Provider for PostcodeAPI.nu
This package is auto-updated.
Last update: 2022-01-09 23:01:35 UTC
README
此包提供了一些基本功能,可用于特定邮政编码API的实现。此包本身不提供对邮政编码数据的直接访问。
安装
-
安装基本包
composer require devmobgroup/postcodes
-
选择邮政编码提供商或创建自己的。您可以在本存储库中找到我们所有的提供商实现这里。
-
PostcodeAPI.nu(推荐)postcodeapi.nu
composer require devmobgroup/postcodes-postcode-api-nu
-
API Postcode api-postcode.nl
composer require devmobgroup/postcodes-api-postcode
了解更多关于使用和配置这些提供商的信息。
-
使用
所有提供商都实现了ProviderInterface
,该接口有一个查找方法
$provider->lookup(string $postcode, string $number);
此方法返回一个包含Address
实例的数组。此数组永远不会为空,因为相反,NoSuchCombinationException
异常表示未找到结果。
$address = $addresses[0]; $address->getPostcode(); // '3011 ED' $address->getHouseNumber(); // '50' $address->getStreet(); // 'Schiedamsedijk' $address->getCity(); // 'Rotterdam' $address->getProvince(); // 'Zuid-Holland' $address->getLatitude(); // '51.9147442' $address->getLongitude(); // '4.4766394'
还有一个getRaw()
方法,通常包含从提供商检索到的原始数据数组
$address->getRaw(); // ['city' => 'Rotterdam', 'year' => 1990, ...]
此外,lookup()
方法会抛出异常,应捕获这些异常。
use DevMob\Postcodes\Exceptions\NoSuchCombinationException; use DevMob\Postcodes\Exceptions\PostcodesException; try { $provider->lookup('3011ED', '50'); } catch (NoSuchCombinationException $e) { // Combination of postcode and house number not found } catch (PostcodesException $e) { // Catch-all interface for other exceptions. // It's best to always catch these exceptions, because // providers may implement their own exceptions that // are not documented in the ProviderInterface. }
扩展
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。