If you are hosting a webshop or an adminsystem to manage your customers contact details, you might collect the VAT-ID of your commercial clients. Usually you would like them to enter valid ids. Moreover, in some cases you have to check this number.
First of all, you can check the number manually at this address:
http://ec.europa.eu/taxation_customs/vies/
However, it would be perfect, if the system could validate the VAT-ID every time, a new customer creates an account on your website, automatically, for example. Well this can be done.
Just create a new PHP-File and paste the following code. Replace the first variable with the full VAT-ID you would like to check and run the script.
<?php
$vatid = 'ATU12345678'; // replace for the VAT-ID you would like to check
$vatid = str_replace(array(' ', '.', '-', ',', ', '), '', trim($vatid));
$cc = substr($vatid, 0, 2);
$vn = substr($vatid, 2);
$client = new SoapClient("http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl");
if($client){
$params = array('countryCode' => $cc, 'vatNumber' => $vn);
try{
$r = $client->checkVat($params);
if($r->valid == true){
// VAT-ID is valid
} else {
// VAT-ID is NOT valid
}
// This foreach shows every single line of the returned information
foreach($r as $k=>$prop){
echo $k . ': ' . $prop;
}
} catch(SoapFault $e) {
echo 'Error, see message: '.$e->faultstring;
}
} else {
// Connection to host not possible, europe.eu down?
}
?>
For more information and debugging, please read the comments in the code. Enjoy.
If you liked this article, please feel free to share this link. Thank you.