| VIES vat number validation |
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. You will find the PHP-Code below the break.
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.
<?
$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 '<div>'.$k.': '.$prop.'</div>';
}
} 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.
0 comments:
Post a Comment