ammaar23/postcodes-io-sdk
Composer 安装命令:
composer require ammaar23/postcodes-io-sdk
包简介
A simple PHP sdk for Postcodes.io
README 文档
README
A simple PHP sdk for Postcodes.io
Install
Install using composer:
$ composer require ammaar23/postcodes-io-sdk
Documentation
Basic Usage Example
use Ammaar23\Postcodes\Postcode; use Ammaar23\Postcodes\PostcodeException; try { $postcodeService = new Postcode(); $response = $postcodeService->lookup('M60 2LA'); echo $response->admin_district; } catch(PostcodeException $e) { echo $e->getMessage(); } catch(\Exception $e) { echo $e->getMessage(); }
You can catch specific
Ammaar23\Postcodes\PostcodeExceptionand/or catch general\Exceptionto catch any type.
Add/Modify Configuration Parameters
You can look at Guzzle HTTP Request Options to find out the availabe options.
$postcodeService = new Postcode([ 'headers' => [ 'User-Agent' => 'testing/1.0', 'Accept' => 'application/json' ], 'timeout' => 2.0 ]);
Methods
Lookup a postcode
Returns a single postcode entity for a given postcode (case, space insensitive).
// Definition function lookup(string $postcode): stdClass; // Example $postcodeService->lookup('M60 2LA');
Bulk lookup postcodes
Returns a list of matching postcodes and respective available data.
// Definition function lookupBulk(array $postcodes, array $attributes = []): array; // Examples $postcodeService->lookupBulk(['OX49 5NU', 'NE30 1DP']); $postcodeService->lookupBulk( ['OX49 5NU', 'NE30 1DP'], ['postcode', 'longitude', 'latitude'] );
$attributes(not required) is an array attributes to be returned in the result object(s).
Reverse Geocoding
Returns nearest postcodes for a given longitude and latitude.
// Definition function reverseGeocode(float $latitude, float $longitude, array $options = []): array; // Examples $postcodeService->reverseGeocode(51.7923246977375, 0.629834723775309); $postcodeService->reverseGeocode(51.7923246977375, 0.629834723775309, [ 'limit' => 5, 'radius' => 1000 ]);
limit(not required) Limits number of postcodes matches to return. Defaults to 10. Needs to be less than 100.radius(not required) Limits number of postcodes matches to return. Defaults to 100m. Needs to be less than 2,000m.
Bulk Reverse Geocoding
Bulk translates geolocations into Postcodes.
// Definition function reverseGeocodeBulk(array $geolocations, array $attributes = [], int $wideSearch = null): array; // Examples $postcodeService->reverseGeocodeBulk([ ['latitude' => 51.7923246977375, 'longitude' => 0.629834723775309], ['latitude' => 53.5351312861402, 'longitude' => -2.49690382054704, 'radius' => 1000, 'limit' => 5] ]); $postcodeService->reverseGeocodeBulk([ ['latitude' => 51.7923246977375, 'longitude' => 0.629834723775309], ['latitude' => 53.5351312861402, 'longitude' => -2.49690382054704, 'radius' => 1000, 'limit' => 5] ], ['postcode', 'longitude', 'latitude']); $postcodeService->reverseGeocodeBulk([ ['latitude' => 51.7923246977375, 'longitude' => 0.629834723775309], ['latitude' => 53.5351312861402, 'longitude' => -2.49690382054704, 'radius' => 1000, 'limit' => 5] ], ['postcode', 'longitude', 'latitude'], 1000);
- Maximum of 100 geolocations per request.
$attributes(not required) is an array attributes to be returned in the result object(s).$wideSearch(not required) Search up to 20km radius, but subject to a maximum of 10 results.
Random Postcode
Returns a random postcode and all available data for that postcode.
// Definition function random(array $options = []): stdClass; // Examples $postcodeService->random(); $postcodeService->random([ 'outcode' => 'M60' ]);
outcode(not required) Filters random postcodes by outcode. Returns null if invalid outcode.
Validate a postcode
Convenience method to validate a postcode.
// Definition function validate(string $postcode): bool; // Example $postcodeService->validate('M60 2LA');
Validate a postcode format
Convenience method to validate a postcode format.
// Definition function validateFormat(string $postcode): bool; // Example $postcodeService->validateFormat('M60 2LA');
validateFormatvalidates the format only where asvalidatecheck's if it exists in the Postcodes.io database or not.
Nearest postcodes for postcode
Returns nearest postcodes for a given postcode.
// Definition function nearest(string $postcode, array $options = []): array; // Examples $postcodeService->nearest('M60 2LA'); $postcodeService->nearest('M60 2LA', [ 'limit' => 5, 'radius' => 1000 ]);
limit(not required) Limits number of postcodes matches to return. Defaults to 10. Needs to be less than 100.radius(not required) Limits number of postcodes matches to return. Defaults to 100m. Needs to be less than 2,000m.
Autocomplete a postcode partial
Convenience method to return an list of matching postcodes.
// Definition function autocomplete(string $postcode, array $options = []): array; // Examples $postcodeService->autocomplete('M60'); $postcodeService->autocomplete('M60', ['limit' => 5]);
limit(not required) Limits number of postcodes matches to return. Defaults to 10. Needs to be less than 100.
Query for postcode
Submit a postcode query and receive a complete list of postcode matches and all associated postcode data. The result set can either be empty or populated with up to 100 postcode entities.
// Definition function query(string $query, array $options = []): array|null; // Examples $postcodeService->query('M60 2LA'); $postcodeService->query('M60 2LA', ['limit' => 5]);
limit(not required) Limits number of postcodes matches to return. Defaults to 10. Needs to be less than 100.
Testing
$ composer test
OR with coverage:
$ composer test-coverage
License
MIT License © 2019 – Ammaar Latif
ammaar23/postcodes-io-sdk 适用场景与选型建议
ammaar23/postcodes-io-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 836 次下载、GitHub Stars 达 5, 最近一次更新时间为 2019 年 04 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「package」 「php」 「sdk」 「postcodes.io」 「postcodes」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ammaar23/postcodes-io-sdk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ammaar23/postcodes-io-sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ammaar23/postcodes-io-sdk 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Simple ASCII output of array data
Alfabank REST API integration
Package for view storage in laravel
User Approval Laravel Package
PHPUnit Pretty Result Printer
统计信息
- 总下载量: 836
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-04-07