kategorijā Noderīgi, Skripti

Simple PHP script for VIES VAT number validation

I have created simple VIES VAT number validation script. Script allows to upload previously prepared CSV with countryCode and vatNumber and creates HTML table with validation results.

 

CSV example:

countryCode;vatNumber
AT;U20557305
AT;U55343505
AT;U63017835
AT;U63091969
AT;U67414700
AT;U68575437
AT;U73556289
BE;0502674883
BG;106030859
BG;115100705

PHP script:

<!-- 
  Author: Karlis Rozenbergs
  Blog: drinkits.optimisti.lv
  Based on http://stackoverflow.com/a/29955950
-->
<html>
  <head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js"></script>
    <style>
      html, body {
        width: 100%;
    }

    /* Tables */
    table {
        position: relative;
        //display: block;
        margin: 10px auto;
        padding: 0;
        width: 100%;
        height: auto;
        border-collapse: collapse;
        text-align: center;
    }

    table thead tr {
        background-color: green;
        font-weight: bold;
    }

    table tr {
        margin: 0;
        padding: 0;
        border: 0;
        border: 1px solid #999;
        width: 100%;
    }

    table tr td {
        margin: 0;
        padding: 4px 8px;
        border: 0;
        border: 1px solid #999;
        
    }
    </style>
    <script type="text/javascript">

    $(document).ready(function(){
        $('#tableID td').each(function(){
            if ($(this).text() == 'false') {
                $(this).css('background-color','red');
            }
        });
    });

    </script>
  </head>
  <body>
    <form action="" method="post" enctype="multipart/form-data">
        <input type="file" name="file">
        <button type="submit" name="import">Import</button>
    </form>
<?php
DEFINE('VIES_URL', 'http://ec.europa.eu/taxation_customs/vies/services/checkVatService');
/**
 * VIES VAT number validation
 * Based on http://stackoverflow.com/a/29955950
 *
 * @author Eugen Mihailescu
 *
 * @param string $countryCode
 * @param string $vatNumber
 * @param int $timeout
 */
function viesCheckVAT($countryCode, $vatNumber, $timeout = 30) {
    $response = array();
    $pattern = '/<(%s).*?>([\s\S]*)<\/\1/';
    $keys = array('countryCode', 'vatNumber', 'requestDate', 'valid', 'name', 'address');
    $content = "<s11:Envelope xmlns:s11='http://schemas.xmlsoap.org/soap/envelope/'>
  <s11:Body>
    <tns1:checkVat xmlns:tns1='urn:ec.europa.eu:taxud:vies:services:checkVat:types'>
      <tns1:countryCode>%s</tns1:countryCode>
      <tns1:vatNumber>%s</tns1:vatNumber>
    </tns1:checkVat>
  </s11:Body>
</s11:Envelope>";
    $opts = array('http' => array('method' => 'POST', 'header' => "Content-Type: text/xml; charset=utf-8; SOAPAction: checkVatService", 'content' => sprintf($content, $countryCode, $vatNumber), 'timeout' => $timeout));
    $ctx = stream_context_create($opts);
    $result = file_get_contents(VIES_URL, false, $ctx);
    if (preg_match(sprintf($pattern, 'checkVatResponse'), $result, $matches)) {
        foreach ($keys as $key) preg_match(sprintf($pattern, $key), $matches[2], $value) && $response[$key] = $value[2];
    }
    return $response;
}

if (isset($_POST['import'])) {
    if ((isset($_FILES['file']) && is_array($_FILES['file']))) {
        $csv = $_FILES['file'];
        if (isset($csv['tmp_name']) && !empty($csv['tmp_name'])) {
            $finfo = finfo_open(FILEINFO_MIME_TYPE);
            $mime = finfo_file($finfo, $csv['tmp_name']);
            finfo_close($finfo);
            $allowed_mime = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain');
            if (in_array($mime, $allowed_mime) && is_uploaded_file($csv['tmp_name'])) {
                $f = fopen($csv['tmp_name'], 'r');
                fgetcsv($f);
                $data = array();
                while (!feof($f)) {
                    $data[] = fgetcsv($f, null, ';');
                }
                foreach ($data as $array) {
                    $dati[] = viesCheckVAT($array[0], $array[1]);
                }
                if (count($dati) > 0):
                    $it = 0; ?>
                <table id="tableID">
                  <thead>
                    <tr>
                      <th>ID</th>
                      <th><?php echo implode('</th><th>', array_keys(current($dati))); ?></th>
                    </tr>
                  </thead>
                  <tbody>
                <?php foreach ($dati as $row):
                        array_map('htmlentities', $row); ?>
                    <tr>
                      <td><?php $it++;
                        echo $it; ?></td>
                      <td><?php echo implode('</td><td>', $row); ?></td>
                    </tr>
                <?php
                    endforeach; ?>
                  </tbody>
                </table>
                <?php
                endif;
                fclose($f);
                die;
            }
        }
    }
}
?>
  </body>
</html>

Test animation

Komentēt

Komentēt