travoltron/zipcode

全球邮编地址搜索器。

v1.5.3 2016-03-15 18:47 UTC

This package is not auto-updated.

Last update: 2024-09-18 10:04:23 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服务可能比这些更好,请创建一个包含该信息的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
  • 使用驼峰式获取器
$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并添加服务提供者

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

和外观

'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许可 - 有关详细信息,请参阅LICENSE文件

贡献

欢迎pull请求和issue。