Skip to content

Instantly share code, notes, and snippets.

@GreatPotato
Created December 4, 2013 10:37
Show Gist options
  • Save GreatPotato/7785541 to your computer and use it in GitHub Desktop.
Save GreatPotato/7785541 to your computer and use it in GitHub Desktop.
Validate a VAT number
<?php
class vat
{
function check_vat($vat_no,$state_code)
{
$states = array(
27 => 'RO',
1 => 'AT',
2 => 'BE',
3 => 'BG',
4 => 'CY',
5 => 'CZ',
6 => 'DE',
7 => 'DK',
8 => 'EE',
9 => 'EL',
10 => 'ES',
11 => 'FI',
12 => 'FR',
13 => 'GB',
14 => 'HU',
15 => 'IE',
16 => 'IT',
17 => 'LT',
18 => 'LU',
19 => 'LV',
20 => 'MT',
21 => 'NL',
22 => 'PL',
23 => 'PT',
24 => 'SE',
25 => 'SI',
26 => 'SK'
);
$found = array_search($state_code,$states);
if ( $found == 0 ) {
echo "FOUND - $found";
return(false);
die();
}
define('POSTURL', 'http://ec.europa.eu/taxation_customs/vies/viesquer.do');
define('POSTVARS', "ms=$state_code&iso=$state_code&vat=$vat_no");
$curlObject = curl_init(POSTURL);
curl_setopt($curlObject, CURLOPT_HEADER,0);
curl_setopt($curlObject, CURLOPT_POST, 1);
curl_setopt($curlObject, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlObject, CURLOPT_POSTFIELDS, POSTVARS);
$response = curl_exec($curlObject);
preg_match('/Yes, valid VAT number/i', $response, $matches);
if ( isset($matches[0])) {
return(true);
}
curl_close($curlObject);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment