adamhebby / php-countries
PHP Countries 是一个提供优雅语法来访问国家数据的库。
v1.1.0
2023-02-03 20:17 UTC
Requires
- php: ^8.1
Requires (Dev)
- phpunit/phpunit: ^9.5
- satooshi/php-coveralls: ^1.0
This package is auto-updated.
Last update: 2024-08-30 01:39:42 UTC
README
PHP Countries 是一个提供优雅语法来访问国家数据的库。
分支自
从 rapidwebltd/php-countries 分支,升级到 PHP 8.1,包括只读属性和最新的国家数据。
安装
您可以通过 Composer 安装 PHP Countries,如下所示。
composer require adamhebby/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)#456 (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(3) "+44" } ["capital"]=> string(6) "London" ["region"]=> string(6) "Europe" ["subregion"]=> string(15) "Northern Europe" ["latitude"]=> float(54) ["longitude"]=> float(-2) ["areaInKilometres"]=> float(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... } } */