Skip to content

Instantly share code, notes, and snippets.

@drahosistvan
Created March 9, 2017 20:53
Show Gist options
  • Save drahosistvan/1d0d2654a1d122996bd8db44fcca4f88 to your computer and use it in GitHub Desktop.
Save drahosistvan/1d0d2654a1d122996bd8db44fcca4f88 to your computer and use it in GitHub Desktop.
EU VAT Number validation
<?php
$pattern = "/^(AT|B[GE]|C[YZ]|D[EK]|E[ELS]|F[IR]|GB|H[RU]|I[ET]|L[TUV]|MT|NL|P[LT]|RO|S[EIK])([A-Z]|[0-9])+$/";
$vatnumber = strtoupper($_REQUEST['q']);
$value = $vatnumber;
if (preg_match($pattern, $vatnumber)){
// Create validation request to VIES service
$country_code = substr($vatnumber, 0, 2);
$vatnum = substr($vatnumber, 2, strlen($vatnumber)-2);
$client = new SoapClient("http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl");
$response = (array)$client->checkVat(array('countryCode' => $country_code,'vatNumber' => $vatnum));
if ($response['valid']) {
// VAT number is valid
// $response holds extra information about the company
} else {
// VAT number is invalid
}
} else {
// Invalid VAT number format
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment