ps-jf / postcode-anywhere
Laravel API包装器,用于PCA Predict(原Postcode Anywhere)
Requires
- php: ^7.1
- laravel/framework: ^7.3|^v8.40.0
Requires (Dev)
- roave/security-advisories: dev-latest
This package is auto-updated.
Last update: 2024-09-17 17:28:00 UTC
README
此包装器将调用Loqate网络服务的流程简化为在您的Laravel应用程序中的一行代码!
依赖关系
此包需要在您的系统上安装PHP cURL扩展。
内容
安装
使用Composer引入此包
composer require ps-jf/postcode-anywhere
注意:如果您正在使用Laravel 5.5,则以下步骤是不必要的。此包支持Laravel 包发现。
在config/app.php
中包含服务提供者。
'providers' => [ ... PostcodeAnywhere\PAServiceProvider, ],
在config/app.php
中包含外观。
'aliases' => [ ... 'PA' => PostcodeAnywhere\PAFacade::class, ]
配置
此包支持配置。
您可以使用以下命令发布配置文件:
php artisan vendor:publish --provider="PostcodeAnywhere\PAServiceProvider" --tag="config"
发布后,的config/postcodeanywhere.php
配置文件包含
<?php return [ /* |-------------------------------------------------------------------------- | Default parameters |-------------------------------------------------------------------------- | | Service key - required | The key to use to authenticate to the service | String - AA11-AA11-AA11-AA11 */ 'params' => [ 'key' => env('PCA_KEY', 'AA11-AA11-AA11-AA11'), ], /* |-------------------------------------------------------------------------- | Service URL |-------------------------------------------------------------------------- */ 'url' => 'https://services.postcodeanywhere.co.uk/', /* |-------------------------------------------------------------------------- | Registered Web Services |-------------------------------------------------------------------------- | @see http://www.postcodeanywhere.co.uk/support/webservices/ */ 'services' => [ 'find' => [ // PostcodeAnywhere Interactive RetrieveBySortcode (v1.00) 'BankAccountValidation' => 'BankAccountValidation/Interactive/RetrieveBySortcode/v1.00/', // PostcodeAnywhere Interactive Find (v1.10) 'PAInteractiveFind' => 'PostcodeAnywhere/Interactive/Find/v1.10/', // PostcodeAnywhere Interactive FindByPostcode (v1.00) 'FindByPostcode' => 'PostcodeAnywhere/Interactive/FindByPostcode/v1.00/', // CapturePlus Interactive Find (v2.10) 'CPInteractiveFind' => 'CapturePlus/Interactive/Find/v2.10/', // PostcodeAnywhere Interactive FindByAreaId (v1.00) 'FindByAreaId' => 'PostcodeAnywhere/Interactive/FindByAreaId/v1.00/', // PostcodeAnywhere Interactive FindByBuilding (v1.00) 'FindByBuilding' => 'PostcodeAnywhere/Interactive/FindByBuilding/v1.00/', // PostcodeAnywhere Interactive FindByLocalityId (v1.00) 'FindByLocalityId' => 'PostcodeAnywhere/Interactive/FindByLocalityId/v1.00/', // PostcodeAnywhere Interactive FindByOrganisation (v1.00) 'FindByOrganisation' => 'PostcodeAnywhere/Interactive/FindByOrganisation/v1.00/', // PostcodeAnywhere Interactive FindByPartialPostcode (v1.00) 'FindByPartialPostcode' => 'PostcodeAnywhere/Interactive/FindByPartialPostcode/v1.00/', // PostcodeAnywhere Interactive FindByParts (v1.00) 'FindByParts' => 'PostcodeAnywhere/Interactive/FindByParts/v1.00/', // PostcodeAnywhere Interactive FindByPoBox (v1.00) 'FindByPoBox' => 'PostcodeAnywhere/Interactive/FindByPoBox/v1.00/', // PostcodeAnywhere Interactive FindByStreet (v1.00) 'FindByStreet' => 'PostcodeAnywhere/Interactive/FindByStreet/v1.00/', // PostcodeAnywhere Interactive FindByStreetId (v1.00) 'FindByStreetId' => 'PostcodeAnywhere/Interactive/FindByStreetId/v1.00/', // PostcodeAnywhere Interactive FindStreets (v1.00) 'FindStreets' => 'PostcodeAnywhere/Interactive/FindStreets/v1.00/', // PostcodeAnywhere Interactive ListAliases (v1.10) 'ListAliases' => 'PostcodeAnywhere/Interactive/ListAliases/v1.10/', // PostcodeAnywhere Interactive ListAreas (v1.00) 'ListAreas' => 'PostcodeAnywhere/Interactive/ListAreas/v1.00/', // PostcodeAnywhere Interactive ListCounties (v1.00) 'ListCounties' => 'PostcodeAnywhere/Interactive/ListCounties/v1.00/', ], 'retrieve' => [ // PostcodeAnywhere Interactive RetrieveById (v1.30) 'RetrieveById' => 'PostcodeAnywhere/Interactive/RetrieveById/v1.30/', // PostcodeAnywhere Interactive RetrieveByAddress (v1.20) 'RetrieveByAddress' => 'PostcodeAnywhere/Interactive/RetrieveByAddress/v1.20/', // CapturePlus Interactive Retrieve (v2.10) 'CPInteractiveRetrieve' => 'CapturePlus/Interactive/Retrieve/v2.10/', // PostcodeAnywhere Interactive RetrieveByIdWithEmail (v1.20) 'RetrieveByIdWithEmail' => 'PostcodeAnywhere/Interactive/RetrieveByIdWithEmail/v1.20/', // PostcodeAnywhere Interactive RetrieveByParts (v1.00) 'RetrieveByParts' => 'PostcodeAnywhere/Interactive/RetrieveByParts/v1.00/', // PostcodeAnywhere Interactive RetrieveByPostcodeAndBuilding (v1.30) 'RetrieveByPostcodeAndBuilding' => 'PostcodeAnywhere/Interactive/RetrieveByPostcodeAndBuilding/v1.30/', // PostcodeAnywhere Interactive RetrieveChanges (v1.00) 'RetrieveChanges' => 'PostcodeAnywhere/Interactive/RetrieveChanges/v1.00/', // PostcodeAnywhere Interactive RetrieveHistoryById (v1.00) 'RetrieveHistoryById' => 'PostcodeAnywhere/Interactive/RetrieveHistoryById/v1.00/', ], ], /* |-------------------------------------------------------------------------- | End point |-------------------------------------------------------------------------- */ 'endpoint' => [ 'xml' => 'xmle.ws?', 'xmla' => 'xmla.ws?', 'json' => 'json.ws?', 'jsonp' => 'json2.ws?', 'json3' => 'json3.ws?', 'json extra' => 'json3ex.ws?', 'csv' => 'csv.ws?', 'tsv' => 'tsv.ws?', 'dataset' => 'dataset.ws?', 'recordset' => 'recordset.ws?', 'htmltable' => 'htmltable.ws?', 'image' => 'image.ws?', 'pdf' => 'pdf.ws?', 'psv' => 'psv.ws?', ], ];
配置文件有一个名为'services'的多维数组,用于通过类型定义网络服务;'find'和'retrieve'。
您可以自由添加/删除其他Loqate / PCA Predict服务。
用法
以下是一个调用给定邮编查找地址记录的示例。
在您的应用程序中,使用数组参数调用\PA::getResponse( array $param )
。
$param = [ 'action' => 'Web Service', 'parameters' => 'array of parameters for Web Service called' ];
示例
$param = [ 'find' => 'FindByPostcode', // perform 'find' action calling 'FindByPostcode' web service 'param' => ['postcode'=>'SW1A 1AA', 'endpoint'=>'json'] // parameters for web service called ];
注意:如果省略,则默认为endpoint
为json
。
详细示例
您可以有服务类,例如Loqate
,在类中,您可以在__construct
或__invoke
魔术方法中做大量工作。以下是一个示例:
<?php namespace App\Services; class Loqate { /** * @var \Illuminate\Support\Collection */ public $addresses; /** * Retrieve an address by it's postcode. * * @param string $postcode * * @return array */ public function __construct($postcode) { // Build an addresses collection $addresses = collect(); // Step 1: Search by postcode to get Loqate's internal ID. $param = [ 'find' => 'FindByPostcode', 'param' => [ 'postcode' => $postcode, 'endpoint' => 'json', ], ]; // JSON Decode the returned response into an array $findResponse = json_decode(\PA::getResponse($param), true); // Loop through the returned array foreach ($findResponse as $findItem) { if (array_key_exists('Id', $findItem)) { // Step 2: Retrieve the full address by it's ID. $param = [ 'retrieve' => 'RetrieveById', 'param' => [ 'id' => $findItem['Id'], ], ]; // JSON Decode the returned response into an array $retrieveResponse = json_decode(\PA::getResponse($param), true); // Loop through the returned array foreach ($retrieveResponse as $item) { // Push to the collection $addresses->push($item); } } } // Convert all keys to snake case $addresses = $addresses->map(function ($value, $key) { return collect($value)->keyBy(function ($value, $key) { return snake_case($key); }); }); $this->addresses = $addresses; } /** * Retrieve the Addresses Property as an array. * * @return array */ public function toArray() { return $this->addresses->toArray(); } /** * Retrieve the Addresses Property unmodified. * * @return \Illuminate\Support\Collection */ public function get() { return $this->addresses; } }
要使用上述服务类,您可以执行如下操作
$postcode = 'WR5 3DA'; $addresses = new App\Services\Loqate($postcode); return $addresses->toArray();
有关更多信息,请参阅Loqate的API文档PostcodeAnywhere Interactive FindByPostcode (v1.00),了解所需参数和响应。
支持
如果您发现错误或有任何建议,请在GitHub上打开问题。
许可
Laravel API包装器针对Loqate / PCA Predict(原Postcode Anywhere)是在MIT许可证下发布的。有关详细信息,请参阅捆绑的LICENSE文件。