rapidwebltd / php-countries
PHP Countries 是一个库,它提供了一种优雅的语法来处理国家数据。
v1.0.5
2018-03-21 11:43 UTC
Requires
- php: ^7.0
- mledoze/countries: ^1.8
Requires (Dev)
- phpunit/phpunit: ^5.7
- satooshi/php-coveralls: ^2.0
README
PHP Countries 是一个库,它提供了一种优雅的语法来处理国家数据。
安装
您可以通过Composer安装PHP Countries,如下所示。
composer require rapidwebltd/php-countries
使用方法
要使用PHP Countries,您必须创建一个新的Countries
对象。
use RapidWeb\Countries\Countries; $countries = new Countries;
然后,您可以调用该对象的各种方法来获取国家数据。
检索所有国家
您可以轻松检索所有国家的数组并遍历它们,如下所示。
foreach($countries->all() as $country) { var_dump($country->name.' - '.$country->officialName); }
按名称检索国家
可以从国家的官方名称或通用名称检索国家详情。
var_dump($countries->getByName('United Kingdom')); /* object(RapidWeb\Countries\Country)#4869 (16) { ["name"]=> string(14) "United Kingdom" ["officialName"]=> string(52) "United Kingdom of Great Britain and Northern Ireland" ["topLevelDomains"]=> array(1) { [0]=> string(3) ".uk" } ["isoCodeAlpha2"]=> string(2) "GB" ["isoCodeAlpha3"]=> string(3) "GBR" ["isoCodeNumeric"]=> string(3) "826" ["languages"]=> array(1) { [0]=> string(7) "English" } ["languageCodes"]=> array(1) { [0]=> string(3) "eng" } ["currencyCodes"]=> array(1) { [0]=> string(3) "GBP" } ["callingCodes"]=> array(1) { [0]=> string(2) "44" } ["capital"]=> string(6) "London" ["region"]=> string(6) "Europe" ["subregion"]=> string(15) "Northern Europe" ["latitude"]=> int(54) ["longitude"]=> int(-2) ["areaInKilometres"]=> int(242900) } */
按ISO 3166-1代码检索国家
您可以通过ISO 3166-1代码获取一个国家的数据。2位、3位和数字变体均接受。
var_dump($countries->getByIsoCode('USA')); /* object(RapidWeb\Countries\Country)#4693 (16) { ["name"]=> string(13) "United States" ["officialName"]=> string(24) "United States of America" // etc... } */
按讲的语言检索国家
提供一种语言,将返回一个数组,包含所有讲该语言的国家。您可以提供语言名称或代码。
var_dump($countries->getByLanguage('German')); /* array(5) { [0]=> object(RapidWeb\Countries\Country)#4913 (16) { ["name"]=> string(7) "Belgium" ["officialName"]=> // etc... } [1]=> object(RapidWeb\Countries\Country)#4883 (16) { ["name"]=> string(7) "Germany" ["officialName"]=> string(27) "Federal Republic of Germany" // etc... } [2]=> object(RapidWeb\Countries\Country)#4826 (16) { ["name"]=> string(13) "Liechtenstein" ["officialName"]=> string(29) "Principality of Liechtenstein" // etc... } [3]=> object(RapidWeb\Countries\Country)#4808 (16) { ["name"]=> string(10) "Luxembourg" ["officialName"]=> string(25) "Grand Duchy of Luxembourg" // etc... } [4]=> object(RapidWeb\Countries\Country)#4871 (16) { ["name"]=> string(7) "Namibia" ["officialName"]=> string(19) "Republic of Namibia" // etc... } } */