This is a simple PHP library to help you deal with Europe's VAT rules.
- Fetch VAT rates for any EU member state using ibericode/vat-rates.
- Validate VAT numbers (by format and/or existence)
- Validate ISO-3316 alpha-2 country codes
- Determine whether a country is part of the EU
- Geo-locate IP addresses
PHP version 8.2 or higher with the CURL and JSON extension is required.
For VAT number existence checking, the PHP SOAP extension is required as well.
To get the latest stable version, install the package using Composer:
composer require ibericode/vatThis library exposes 4 main classes to interact with: Rates, Countries, Validator and Geolocator.
This package relies on a community maintained repository of vat rates. We invite you to toggle notifications for that repository and contribute changes to VAT rates in your country once they are announced.
$rates = newIbericode\Vat\Rates('/path-for-storing-cache-file.txt'); $rates->getRateForCountry('NL'); // 21$rates->getRateForCountry('NL', 'standard'); // 21$rates->getRateForCountry('NL', 'reduced'); // 9$rates->getRateForCountryOnDate('NL', new \DateTime('2010-01-01'), 'standard'); // 19This fetches rate from ibericode/vat-rates and stores a local copy that is periodically refreshed (once every 12 hours by default).
Validating a VAT number:
$validator = newIbericode\Vat\Validator(); $validator->validateVatNumberFormat('NL203458239B01'); // true (checks format)$validator->validateVatNumber('NL203458239B01'); // false (checks format + existence)Validating an IP address:
$validator = newIbericode\Vat\Validator(); $validator->validateIpAddress('256.256.256.256'); // false$validator->validateIpAddress('8.8.8.8'); // trueValidating an ISO-3166-1-alpha2 country code:
$validator = newIbericode\Vat\Validator(); $validator->validateCountryCode('DE'); // true$validator->validateCountryCode('ZZ'); // false$countries = newIbericode\Vat\Countries(); // access country name using array accessecho$countries['NL']; // Netherlands// loop over countriesforeach ($countriesas$code => $name){// ... } // check if country is in EU$countries->isCountryCodeInEU('NL'); // true$countries->isCountryCodeInEU('US'); // falseThis library includes a simple geo-location service using ip2c.org or ip2country.info (deprecated as of Dec 2022).
$geolocator = newIbericode\Vat\Geolocator(); $geolocator->locateIpAddress('8.8.8.8'); // USTo use ip2c.org explicitly.
$geolocator = newIbericode\Vat\Geolocator('ip2c.org'); $geolocator->locateIpAddress('8.8.8.8'); // USIf you need to use this package in a Symfony environment, check out ibericode/vat-bundle.
ibericode/vat is licensed under the MIT License.