PHP code example of webatvantage / vies-vat-api-client

1. Go to this page and download the library: Download webatvantage/vies-vat-api-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

webatvantage / vies-vat-api-client example snippets




use Webatvantage\Vies\Vies;
use Webatvantage\Vies\Api\VatEuropeApi;


$vatResult = $vat = $viesService->validateVat(
    'BE', // Country code
    '0203430576' // VAT ID
);

$vatResult->valid ? 'Valid' : 'Not valid'

try
{
    $valid = $viesService->validateVatFormat(
        'BE', // Country code
        '0203430576' // VAT ID
    );
}
catch (ViesException $exception)
{
    $reason = $exception->getMessage();
}

function validateVat(string $countryCode, string $vatNumber, ?string &$error = null): bool
{
    try
    {
        $vat = $viesService->validateVat($countryCode, $vatNumber);
    }
    catch (InvalidCountryCodeException $exception)
    {
        $error = $exception->getMessage();

        return false;
    }
    catch (InvalidVatNumberFormatException $exception)
    {
        $error = $exception->getMessage();

        return false;
    }
    catch (ViesServiceException $exception)
    {
        // Thrown when VIES is unreachable, or when the country is no longer
        // served by VIES (e.g. the United Kingdom since Brexit).
        $error = $exception->getMessage();

        return false;
    }

    if ($vat->valid === false)
    {
        $error = 'The provided VAT number is not valid';

        return false;
    }

    return true;
}


use Webatvantage\Vies\Vies;

$vatNumber = 'BE0203.430.576';

$vatNumber = Vies::normalizeVat($vatnumber); // BE0203430576



use Webatvantage\Vies\Vies;

$vatNumber = 'BE0203430576';

['country' => $countryCode, 'id' => $vatNumber] = Vies::splitVatId($vatnumber); // ['country' => 'BE', 'id' => '203430576'];



use Webatvantage\Vies\Countries;

$countries = Countries::europeanCountries();