arthurydalgo/zipcode
Composer 安装命令:
composer require arthurydalgo/zipcode
包简介
A worldwide address-by-zipcode searcher.
README 文档
README
A Laravel WorldWide ZIP code searcher
You can use it in Laravel:
ZipCode::setCountry('US'); return Response::make( ZipCode::find('10006') );
Or outside it:
$z = new PragmaRX\ZipCode\ZipCode; return $z->find('20250030')->toArray();
It automatically renders a JSON if you try to access it as string, but you still can:
$result = ZipCode::find('10006'); $json = $result->toJson(); $array = $result->toArray();
Select your preferred web service:
ZipCode::setPreferredWebService('Zippopotamus');
Get a web service by name, change things on it and find an address/city with it:
$webService = ZipCode::getWebServiceByName('Zippopotamus'); $webSerivice->setUrl('http://api.zippopotam.ca'); return ZipCode::find('20250030', $webService);
Create a new web service and add it to the list:
$webService = new PragmaRX\ZipCode\Support\WebService; $webSerivice->setUrl('http://api.zippopotam.ca'); $webSerivice->setQuery('/%country%/%zip_code%'); ZipCode::addWebService($webService);
Change the user agent Guzzle will use to access the web service:
ZipCode::setUserAgent('Googlebot/2.1 (+http://www.google.com/bot.html)');
How much time it took to find a zip?:
$result = ZipCode::find('0200'); echo $result->getTimer();
Get a list of all available countries:
$array = ZipCode::getAvailableCountries();
Dynamically change query parameters, so if you have a Geonames login, you can set it by doing:
ZipCode::setQueryParameter('geonames_username', 'yourusername');
Web Services
This package uses web services all around the world to provide addresses and cities information. There are at least 2 web services available to all countries (Brazil currently has 6), if ZipCode cannot access one or doesn't find a zip on it, it automatically falls back to the others. If you know of any other web services available that could be better than those, please create an issue or PR with it.
Result
This is an example of what you get when you search a Zip with it:
{ country_id:"CH", country_name:"Switzerland", zip_code:"1005", web_service:"Geonames", timer:"0.7808", service_query_url:"http://api.geonames.org/postalCodeSearch?country=CH&postalcode=1005&username=demo", addresses:[ { postal_code:"1005", state_name:"Canton de Vaud", state_id:"VD", city:"Lausanne", latitude:"46.51985", longitude:"6.64252", department:"District de Lausanne", department_id:"2225", district:"Lausanne" } ], result_raw:{ totalResultsCount:"1", code:{ postalcode:"1005", name:"Lausanne", countryCode:"CH", lat:"46.51985", lng:"6.64252", adminCode1:"VD", adminName1:"Canton de Vaud", adminCode2:"2225", adminName2:"District de Lausanne", adminCode3:"5586", adminName3:"Lausanne" } }, success:true }
ZipCode returns a PragmaRX\ZipCode\Support\Result object and all properties can be accessed:
- As array
- As string, which will make it return a JSON
- Using camel cased getters:
$result->getWebService(); $result->getCountryName();
Laravel Form Example
This is an unconventionally hacked Laravel router which renders a form to query zips on a selected country:
Route::any('zipcode', function() { echo Form::open(array('url' => 'zipcode')) . Form::select('country', ZipCode::getAvailableCountries(), Input::get('country')) . Form::text('zipcode', Input::get('zipcode')) . Form::submit('go!') . Form::close(); if (Input::get('country')) { ZipCode::setCountry(Input::get('country')); ZipCode::setQueryParameter('geonames_username', 'demo'); echo '<pre>'; var_dump(ZipCode::find(Input::get('zipcode'))->toArray()); echo '</pre>'; } });
Available countries
There are web services tested for the following countries:
- Argentine (AR)
- Australia (AU)
- Brazil (BR)
- Canada (CA)
- Czech Republic (CZ)
- France (FR)
- Germany (DE)
- Great Britain (GB)
- India (IN)
- Italy (IT)
- Japan (JP)
- Lithuania (LT)
- Mexico (MX)
- Pakistan (PK)
- Poland (PL)
- Portugal (PT)
- Russia (RU)
- South Africa (ZA)
- Spain (ES)
- Switzerland (CH)
- Turkey (TR)
- United States (US)
If you need a different one, please ask or just send a pull request with it.
Requirements
- Laravel 4.1+ or 5+
- PHP 5.4+
Installing
Install it using Composer:
composer require "pragmarx/zipcode"
Edit your app/config/app.php and add the Service Provider
'PragmaRX\ZipCode\Vendor\Laravel\ServiceProvider',
And the Facade
'ZipCode' => 'PragmaRX\ZipCode\Vendor\Laravel\Facade',
Using It
Instantiate it directly
use PragmaRX\ZipCode\ZipCode;
$zipcode = new ZipCode();
return $zipcode->generateSecretKey()
In Laravel you can use the IoC Container and the contract
$zipcode = app()->make('PragmaRX\ZipCode\Contracts\ZipCode');
return $zipcode->find('20250-030')
Or Method Injection, in Laravel 5
use PragmaRX\ZipCode\Contracts\ZipCode;
class WelcomeController extends Controller {
public function generateKey(ZipCode $zipcode)
{
return $zipcode->find('20250-030');
}
}
About Geonames
This is a really nice service and you should use it as your first option, but for it to be free (for 30,000 credits/day) you have to create an user account and enable the free webservices. And configure ZipCode to use your username:
ZipCode::setCountry('GB');
ZipCode::setQueryParameter('geonames_username', 'yourusername');
ZipCode::find('L23YL');
And you can also use config.php to set it:
return array(
...
'query_parameters' => array(
'geonames_username' => 'demo',
)
);
Author
License
ZipCode is licensed under the BSD 3-Clause License - see the LICENSE file for details
Contributing
Pull requests and issues are more than welcome.
arthurydalgo/zipcode 适用场景与选型建议
arthurydalgo/zipcode 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 818 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 06 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「address」 「laravel」 「zip」 「zip code」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 arthurydalgo/zipcode 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 arthurydalgo/zipcode 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 arthurydalgo/zipcode 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Maps in minutes. Powered by the Google Maps API.
Redirects TYPO3 visitors automatic or with a suggestlink to another language and/or root page.
Allow packages to be installed from a repository or tarball bundle that have multiple packages in the sub-folders
Module adding custom shipping attribute for what3words address
Iteration tools for PHP
Convert and operate with FIPS codes for states, counties, etc.
统计信息
- 总下载量: 818
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2023-06-26