Universal country data library with ISO codes, phone codes, flags, currencies, and languages.
Install via Composer:
composer require countrykit/countrykitOr add to your composer.json:
{
"require": {
"countrykit/countrykit": "^1.0"
}
}- PHP 7.2 or higher
- No other dependencies
- 250 countries and territories with complete data
- ISO 3166-1 alpha-2, alpha-3, and numeric codes
- Phone calling codes
- SVG and emoji flags
- Currencies with symbols
- Languages
- Regions and subregions
- Fast O(1) lookups by ISO codes
- Zero dependencies
- PSR-4 autoloading
<?php
require 'vendor/autoload.php';
use CountryKit\CountryKit;// Get by ISO 3166-1 alpha-2 (2-letter)
$usa = CountryKit::getCountryByIso2('US');
echo $usa['name']; // "United States"
echo $usa['calling_code']; // "+1"
// Get by ISO 3166-1 alpha-3 (3-letter)
$gbr = CountryKit::getCountryByIso3('GBR');
echo $gbr['name']; // "United Kingdom"// Countries with +1 calling code
$countries = CountryKit::getCountriesByCallingCodeValue('+1');
foreach ($countries as $country) {
echo $country['name'] . "\n";
}
// Output: United States, Canada// All European countries
$europe = CountryKit::getCountriesByRegion('Europe');
echo count($europe); // Number of European countries
// All countries in Southern Asia
$southAsia = CountryKit::getCountriesBySubregion('Southern Asia');// All countries using Euro
$euroCountries = CountryKit::getCountriesByCurrency('EUR');
foreach ($euroCountries as $country) {
echo $country['name'] . "\n";
}// All English-speaking countries
$englishCountries = CountryKit::getCountriesByLanguage('en');
foreach ($englishCountries as $country) {
echo $country['name'] . "\n";
}// Search by name or native name
$results = CountryKit::searchCountries('united');
foreach ($results as $country) {
echo $country['name'] . "\n";
}
// Output: United States, United Kingdom, United Arab Emirates// All countries (array)
$allCountries = CountryKit::getCountries();
echo count($allCountries);
// Fast lookup arrays
$byIso2 = CountryKit::getCountriesByIso2();
$byIso3 = CountryKit::getCountriesByIso3();
$byPhone = CountryKit::getCountriesByCallingCode();
// All currencies
$currencies = CountryKit::getCurrencies();
// All languages
$languages = CountryKit::getLanguages();
// All calling codes
$callingCodes = CountryKit::getDialCodes();
// All regions
$regions = CountryKit::getAllRegions();
// All subregions
$subregions = CountryKit::getAllSubregions();Each country array contains:
[
'cca2' => 'US', // ISO 3166-1 alpha-2
'cca3' => 'USA', // ISO 3166-1 alpha-3
'ccn3' => '840', // ISO 3166-1 numeric
'name' => 'United States', // Common name
'native_name' => 'United States', // Native name
'calling_code' => '+1', // Phone calling code
'capital' => 'Washington, D.C.', // Capital city
'region' => 'Americas', // Region
'subregion' => 'Northern America', // Subregion
'tld' => '.us', // Top-level domain
'currency' => [ // Currencies (array)
[
'code' => 'USD',
'name' => 'United States Dollar',
'symbol' => '$'
]
],
'languages' => [ // Languages (array)
[
'code' => 'en',
'name' => 'English'
]
],
'flag' => [
'emoji' => '🇺🇸', // Flag emoji
'svg' => 'flags/US.svg' // SVG flag path
]
]getCountryByIso2($code)- Get country by 2-letter ISO codegetCountryByIso3($code)- Get country by 3-letter ISO codegetCountriesByCallingCodeValue($code)- Get countries by phone codegetCountriesByRegion($region)- Get countries by regiongetCountriesBySubregion($subregion)- Get countries by subregiongetCountriesByCurrency($code)- Get countries by currency codegetCountriesByLanguage($code)- Get countries by language codesearchCountries($query)- Search countries by name
getAllRegions()- Get all unique regionsgetAllSubregions()- Get all unique subregionsgetCurrencies()- Get all currencies with detailsgetLanguages()- Get all languages with detailsgetDialCodes()- Get all calling codes with mappings
getCountries()- Get all country datagetCountriesByIso2()- Get array indexed by ISO2getCountriesByIso3()- Get array indexed by ISO3getCountriesByCallingCode()- Get array grouped by phone code
$country = CountryKit::getCountryByIso2('FR');
echo "Country: {$country['name']} {$country['flag']['emoji']}\n";
echo "Capital: {$country['capital']}\n";
echo "Phone: {$country['calling_code']}\n";
echo "Currencies: ";
foreach ($country['currency'] as $curr) {
echo "{$curr['name']} ({$curr['symbol']}) ";
}
echo "\n";$countries = CountryKit::getCountries();
?>
<select name="country">
<?php foreach ($countries as $country): ?>
<option value="<?= $country['cca2'] ?>">
<?= $country['flag']['emoji'] ?> <?= $country['name'] ?>
</option>
<?php endforeach; ?>
</select>function validatePhoneCountry($phoneNumber) {
$dialCodes = CountryKit::getDialCodes();
foreach ($dialCodes as $dialCode) {
if (strpos($phoneNumber, $dialCode['code']) === 0) {
return $dialCode['countries'];
}
}
return null;
}
$matches = validatePhoneCountry('+1-555-1234');
// Returns: ['US', 'CA']function getCountriesForCurrency($currencyCode) {
$countries = CountryKit::getCountriesByCurrency($currencyCode);
$names = array_map(function($c) {
return $c['name'];
}, $countries);
return implode(', ', $names);
}
echo getCountriesForCurrency('EUR');
// Output: Germany, France, Spain, Italy, Netherlands, ...MIT License - see LICENSE file for details
Contributions are welcome! Please feel free to submit a Pull Request.
Data is curated from official sources and updated regularly. Flag SVGs are from public domain sources.