arthurydalgo/zipcode

全球邮编地址搜索器。

0.4 2024-06-13 14:23 UTC

This package is not auto-updated.

Last update: 2024-09-19 16:06:24 UTC


README

Latest Stable Version License

Laravel 全球 ZIP 编码搜索器

您可以在 Laravel 中使用它

ZipCode::setCountry('US');

return Response::make(
    ZipCode::find('10006')
);

或外部使用

$z = new PragmaRX\ZipCode\ZipCode;

return $z->find('20250030')->toArray();

如果您将其作为字符串访问,它会自动渲染 JSON,但您仍然可以这样做

$result = ZipCode::find('10006');

$json = $result->toJson();
$array = $result->toArray();

选择您首选的 Web 服务

ZipCode::setPreferredWebService('Zippopotamus');

通过名称获取 Web 服务,更改其内容并使用它查找地址/城市

$webService = ZipCode::getWebServiceByName('Zippopotamus');

$webSerivice->setUrl('http://api.zippopotam.ca');

return ZipCode::find('20250030', $webService);

创建新的 Web 服务并将其添加到列表中

$webService = new PragmaRX\ZipCode\Support\WebService;

$webSerivice->setUrl('http://api.zippopotam.ca');
$webSerivice->setQuery('/%country%/%zip_code%');

ZipCode::addWebService($webService);

更改 Guzzle 访问 Web 服务时使用的用户代理

ZipCode::setUserAgent('Googlebot/2.1 (+http://www.google.com/bot.html)');

找到邮编需要多长时间?

$result = ZipCode::find('0200');

echo $result->getTimer();

获取所有可用国家的列表

$array = ZipCode::getAvailableCountries();

动态更改查询参数,如果您有 Geonames 登录,您可以设置它,如下所示

ZipCode::setQueryParameter('geonames_username', 'yourusername');

Web 服务

此软件包使用全球的 Web 服务来提供地址和城市信息。对所有国家至少有 2 个 Web 服务可用(目前巴西有 6 个),如果 ZipCode 无法访问其中一个或在该 Web 服务上找不到邮编,它将自动切换到其他 Web 服务。如果您知道其他更好的 Web 服务,请创建一个带有该 Web 服务的 issue 或 PR。

结果

这是您使用它搜索邮编时得到的一个示例

{
   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 返回一个 PragmaRX\ZipCode\Support\Result 对象,所有属性都可以访问

  • 作为数组
  • 作为字符串,这将使其返回 JSON
  • 使用驼峰式 getter
$result->getWebService();
$result->getCountryName();

Laravel 表单示例

这是一个非传统的 Laravel 路由器,它渲染一个表单以查询选定国家的邮编

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>';
    }

});

可用国家

以下国家已测试过 Web 服务

  • 阿根廷 (AR)
  • 澳大利亚 (AU)
  • 巴西 (BR)
  • 加拿大 (CA)
  • 捷克共和国 (CZ)
  • 法国 (FR)
  • 德国 (DE)
  • 大不列颠 (GB)
  • 印度 (IN)
  • 意大利 (IT)
  • 日本 (JP)
  • 立陶宛 (LT)
  • 墨西哥 (MX)
  • 巴基斯坦 (PK)
  • 波兰 (PL)
  • 葡萄牙 (PT)
  • 俄罗斯 (RU)
  • 南非 (ZA)
  • 西班牙 (ES)
  • 瑞士 (CH)
  • 土耳其 (TR)
  • 美国 (US)

如果您需要其他国家,请提出要求或直接发送带有该国家的 pull request。

需求

  • Laravel 4.1+ 或 5+
  • PHP 5.4+

安装

使用Composer安装

composer require "pragmarx/zipcode"

编辑您的 app/config/app.php 并添加 Service Provider

'PragmaRX\ZipCode\Vendor\Laravel\ServiceProvider',

和 Facade

'ZipCode' => 'PragmaRX\ZipCode\Vendor\Laravel\Facade',

使用它

直接实例化它

use PragmaRX\ZipCode\ZipCode;

$zipcode = new ZipCode();

return $zipcode->generateSecretKey()

在 Laravel 中,您可以使用 IoC 容器和合同

$zipcode = app()->make('PragmaRX\ZipCode\Contracts\ZipCode');

return $zipcode->find('20250-030')

或方法注入,在 Laravel 5 中

use PragmaRX\ZipCode\Contracts\ZipCode;

class WelcomeController extends Controller {

	public function generateKey(ZipCode $zipcode)
	{
		return $zipcode->find('20250-030');
	}

}

关于 Geonames

这是一个非常好的服务,您应该将其作为首选选项,但要使其免费(每天 30,000 个积分),您必须 创建用户帐户 启用免费 Web 服务。然后配置 ZipCode 使用您的用户名

ZipCode::setCountry('GB');

ZipCode::setQueryParameter('geonames_username', 'yourusername');

ZipCode::find('L23YL');

您还可以使用 config.php 来设置它

return array(

	...

	'query_parameters' => array(
		'geonames_username' => 'demo',
	)

);

作者

Antonio Carlos Ribeiro

许可

ZipCode 在 BSD 3-Clause 许可证下授权 - 有关详细信息,请参阅 LICENSE 文件

贡献

欢迎 pull request 和 issue。