Download the PHP package mlocati/vat-lib without Composer
On this page you can find all versions of the php package mlocati/vat-lib. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mlocati/vat-lib
More information about mlocati/vat-lib
Files in mlocati/vat-lib
Informations about the package vat-lib
A PHP library to validate and normalize and VAT numbers
This project provides a library that checks and normalizes VAT numbers (including querying the VIES VoW (VAT Information Exchange System - Vies-on-the-web service for European countries).
Requirements
VATLib has these requirements:
- It works with any PHP version starting from 5.5 (PHP 5.5.x, 5.6.x, 7.x, and 8.x are fully supported).
- Very flexible dependencies for querying online services like VIES:
- the SOAP extension is not required
- the library automatically uses these adapters (you only need one, not all):
- the Guzzle library
- the PHP cURL extension
- the Zend HTTP library
- the PHP
http
stream wrapper
- Checking the syntax of VAT numbers of some countries (Netherlands and Slovakia) requires handling of large numbers: on 32 bit systems you may need the
bcmath
or thegmp
PHP extension.
Installation
Manual installation
Download the latest version, unzip it and add these lines in our PHP files:
Installation with Composer
Simply run:
or add these lines to your composer.json
file:
Checking and normalizing VAT numbers
You can use the VATLib\Checker
class to validate and normalize VAT numbers.
You can obtain an instance of it simply with:
You can then use its check
method.
This method accepts one to two arguments: the first argument is the VAT number to be checked, the second argument (optional) is the ISO 3166 alpha-2 code of the Country associated to that VAT number.
Some examples (that lead to the same valid result):
Verifying the validity
$checkResult
provides the isSyntaxValid()
method, which returns:
true
if the VAT number is syntactically validfalse
if the VAT number is syntactically invalidnull
if it's impossible to verify if the VAT number is syntactically valid. Some examples of this case are:- you provided a country code that's not supported (or that's invalid)
- you didn't provide a country code and it's not possible to determine the country associated to the VAT number provided (for example if you call
$checker->check('00159560366')
)
When the VAT number is syntactically valid, the library also queries the VIES service for EU countries.
You can check the VIES response with some code like this:
$checkResult
provides a quick way to check both isSyntaxValid()
and the VIES result: you can use its isValid()
method, which returns:
true
if the VAT number is syntactically valid, and the VIES was not queried or its response is successfulfalse
if the VAT number is syntactically invalid, or the VIES service told us that it's not a validnull
if it's impossible to verify if the VAT number is valid
Retrieving the formatted VAT number
To obtain the normalized VAT number, you can use the getShortVatNumber()
or the getLongVatNumber()
methods of $checkResult
:
Getting VAT number info
You can obtain some details about a VAT number:
The code above would print:
Checking for errors
Unexpected problems may occur when checking VAT numbers (for example, because the VIES service is temporarily not available).
You can check if any errors occurred using the hasExceptions
method of $checkResult
:
Detecting the possible countries given a VAT number
If you have a generic VAT number and you would like to know the possible countries where it may be defined, you can use the getApplicableFormats()
method of the VATLib\Checker
class:
The code above would print:
Checking the status of the VIES service
Manually querying VIES
You can also query VIES directly:
The code above may output:
Customizing the HTTP adapter for VIES
By default, VATLib automatically detects the available adapter, but of course you can specify your own.
For example, if you are using Guzzle and you need to configure a proxy, you can write something like this:
The same can be done using the cURL PHP extension:
The same can be done using the http
stream wrapper:
Supported countries
The library supports these countries:
- all the European Union Countries:
- Austria (
AT
) - Belgium (
BE
) - Bulgaria (
BG
) - Croatia (
HR
) - Cyprus (
CY
) - Czech (
CZ
) - Denmark (
DK
) - Estonia (
EE
) - Finland (
FI
) - France (
FR
) - Germany (
DE
) - Greece (
GR
country code, VAT numbers starting withEL
) - Hungary (
HU
) - Ireland (
IE
) - Italy (
IT
) - Latvia (
LV
) - Lithuania (
LT
) - Luxembourg (
LU
) - Malta (
MT
) - Netherlands (
NL
) - Poland (
PL
) - Portugal (
PT
) - Romania (
RO
) - Slovakia (
SK
) - Slovenia (
SI
) - Spain (
ES
) - Sweden (
SE
)
- Austria (
- Switzerland (both
CHE-123.456.789
and123.456.789
UID numbers) - United Kingdom (
GB
), with the following extension:- for VAT numbers starting with
XI
, the VIES service will be queried accordingly to the Protocol on Ireland and Northern Ireland
- for VAT numbers starting with
- MOSS VAT numbers (
EU
)
All versions of vat-lib with dependencies
ext-json Version *