PHPackages                             happyphper/country - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. happyphper/country

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

happyphper/country
==================

A PHP package containing information about all countries including ISO codes, names, and flags

v1.0.0(1y ago)03MITPHPPHP &gt;=7.4

Since May 28Pushed 1y agoCompare

[ Source](https://github.com/happyphper/country)[ Packagist](https://packagist.org/packages/happyphper/country)[ RSS](/packages/happyphper-country/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (1)Versions (2)Used By (0)

Country PHP Package
===================

[](#country-php-package)

这是一个简单的 PHP 包，提供了所有国家的信息，包括 ISO 代码、名称和国旗。

数据来源
----

[](#数据来源)

本包使用的国家数据来自 [ISO 3166-1](https://www.iso.org/obp/ui/#search) 标准，包括：

- ISO 3166-1 alpha-2 代码（如：CN）
- ISO 3166-1 alpha-3 代码（如：CHN）
- ISO 3166-1 numeric 代码（如：156）
- 国家英文名称

安装
--

[](#安装)

使用 Composer 安装：

```
composer require happyphper/country
```

使用方法
----

[](#使用方法)

### 获取所有国家

[](#获取所有国家)

```
use HappyPhper\Country\CountryCollection;

$collection = new CountryCollection();
$countries = $collection->getAll();
```

### 通过 ISO 代码获取国家

[](#通过-iso-代码获取国家)

```
use HappyPhper\Country\CountryCollection;

$collection = new CountryCollection();

// 通过 ISO 3166-1 alpha-2 代码获取
$country = $collection->getByAlpha2('CN');

// 通过 ISO 3166-1 alpha-3 代码获取
$country = $collection->getByAlpha3('CHN');

// 通过 ISO 3166-1 numeric 代码获取
$country = $collection->getByNumeric('156');
```

### 获取国家信息

[](#获取国家信息)

```
$country = $collection->getByAlpha2('CN');

echo $country->getName();        // 输出: China
echo $country->getAlpha2();      // 输出: CN
echo $country->getAlpha3();      // 输出: CHN
echo $country->getNumeric();     // 输出: 156
echo $country->getFlagUrl();     // 输出: https://flagcdn.com/w320/cn.png
```

### 获取国旗 URL

[](#获取国旗-url)

包支持多种方式获取国旗 URL：

#### 1. 获取默认尺寸的国旗（320px 宽度）

[](#1-获取默认尺寸的国旗320px-宽度)

```
$country = $collection->getByAlpha2('CN');
echo $country->getFlagUrl();     // 输出: https://flagcdn.com/w320/cn.png
```

#### 2. 获取固定宽度的国旗

[](#2-获取固定宽度的国旗)

```
// 支持的宽度：20, 40, 80, 160, 320, 640, 1280, 2560
echo $country->getFlagUrlByWidth(640);  // 输出: https://flagcdn.com/w640/cn.png
```

#### 3. 获取固定高度的国旗

[](#3-获取固定高度的国旗)

```
// 支持的高度：20, 24, 40, 60, 80, 120, 240
echo $country->getFlagUrlByHeight(240);  // 输出: https://flagcdn.com/h240/cn.png
```

#### 4. 获取固定尺寸的国旗

[](#4-获取固定尺寸的国旗)

```
// 支持任意宽度和高度
echo $country->getFlagUrlByDimensions(16, 12);  // 输出: https://flagcdn.com/16x12/cn.png
```

#### 5. 获取不同格式的国旗

[](#5-获取不同格式的国旗)

```
// 支持的格式：png, webp, svg, jpg
echo $country->getFlagUrlByWidth(320, Country::FORMAT_WEBP);  // 输出: https://flagcdn.com/w320/cn.webp
echo $country->getFlagUrlByWidth(320, Country::FORMAT_SVG);   // 输出: https://flagcdn.com/w320/cn.svg
echo $country->getFlagUrlByWidth(320, Country::FORMAT_JPEG);  // 输出: https://flagcdn.com/w320/cn.jpg
```

#### 6. 获取 SVG 格式的国旗

[](#6-获取-svg-格式的国旗)

```
echo $country->getSvgFlagUrl();  // 输出: https://flagcdn.com/cn.svg
```

多语言支持
-----

[](#多语言支持)

本包支持多语言（国际化），可通过设置 locale 获取不同语言的国家名称。 目前已内置支持：英文（en）、简体中文（zh-CN）、繁体中文（zh-TW）。

### 示例

[](#示例)

```
use HappyPhper\Country\CountryCollection;

// 创建中文集合
$collection = new CountryCollection('zh-CN');
$china = $collection->getByAlpha2('CN');
echo $china->getLocalizedName(); // 输出: 中国

// 切换为繁体中文
$collection->setLocale('zh-TW');
echo $china->getLocalizedName(); // 输出: 中國

// 切换为英文
$collection->setLocale('en');
echo $china->getLocalizedName(); // 输出: China
```

如需自定义语言，可在 `resources/lang/` 目录下添加对应的语言文件。

国旗来源
----

[](#国旗来源)

国旗图片来自 [flagcdn.com](https://flagcdn.com)，这是一个免费的国旗 CDN 服务。

许可证
---

[](#许可证)

MIT License

###  Health Score

24

—

LowBetter than 31% of packages

Maintenance46

Moderate activity, may be stable

Popularity3

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity37

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

401d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/eee5b6fe3998ce04be32705a12e157bfd241e8a2f6f1544c094ba172bccc9153?d=identicon)[happyphper](/maintainers/happyphper)

---

Top Contributors

[![baronbool](https://avatars.githubusercontent.com/u/129026827?v=4)](https://github.com/baronbool "baronbool (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/happyphper-country/health.svg)

```
[![Health](https://phpackages.com/badges/happyphper-country/health.svg)](https://phpackages.com/packages/happyphper-country)
```

###  Alternatives

[jalle19/php-yui-compressor

A modern PHP wrapper for the YUI compressor

16980.4k3](/packages/jalle19-php-yui-compressor)[dedi/sylius-seo-plugin

Sylius SEO plugin by Dedi.

28145.3k](/packages/dedi-sylius-seo-plugin)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
