Advertisement
eudemonics

PHP auto-incrementing valid VAT number search over WSDL

Jan 22nd, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.93 KB | None | 0 0
  1. <?php
  2. /* VAT number formats for each specific EU country can be found here:
  3. http://www.pixelgenius.com/vat-id.html
  4. Just change countryCode value to the country of your choice,
  5. and modify the for loop to match that country's algorithm */
  6.  
  7. ini_set('default_socket_timeout',10);
  8.  
  9. /*set 11-digit string to start the search from */
  10. $vatIdinit = '00111120319';
  11. $count = 0;
  12. $match = 0;
  13.  
  14. ?>
  15. <html>
  16. <head>
  17. <title>SEQUENTIAL VAT FINDER</title>
  18. </head>
  19.  
  20. <body>
  21.  
  22. <?php
  23.  
  24. print "<h2>starting search...</h2>";
  25.  
  26. /* Initialize web service with WSDL */
  27. $client = new SoapClient("http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl");
  28.  
  29. echo "<h3>connecting to SOAP..</h3><br />";
  30.  
  31. //if VAT ID is passed through GET paramater in URL
  32. if (isset($_GET["id"]) && ctype_digit($_GET["id"])) {
  33.     $vatId = $_GET["id"];
  34.    
  35.     /* Set parameters for the request */
  36.     $params = array(
  37.       "countryCode" => "IT",
  38.       "vatNumber" => $vatId
  39.     );
  40.    
  41.     /* Web service request */
  42.     $return = $client->checkVat($params);
  43.    
  44.     echo "<b>RAW RESPONSE:</b><br /><br />";
  45.     var_dump($return);
  46.     echo "<br />";
  47.     $valid = $return->valid;
  48.    
  49.     if (!$valid) {
  50.         echo "<br /><h3><b style='color:#600';>".$vatId."</b> is not valid.</h3><br />";
  51.     }
  52.    
  53.     else {
  54.         /* Parse response from web service */
  55.         $countryCode = $return->countryCode;
  56.         $rawvalid = $return->valid;
  57.         $vatNumber = $return->vatNumber;
  58.         $requestDate = substr(($return->requestDate),0,10);
  59.         $name = $return->name;
  60.         $address = $return->address;
  61.        
  62.         print "<br /><br /><b>Valid VAT number found in ".$countryCode.":</b><br />";
  63.         print "<h3 style='color:#600;'><b>".$vatNumber."</b></h3>";
  64.         print "<b>Request Date: </b>".$requestDate."<br />";
  65.         print "<b>Name: </b>".$name."<br />";
  66.         print "<b>Address: </b>".$address."<br /><br />";
  67.     }
  68.     exit();
  69. }
  70.        
  71. else {
  72.     for ($i=0; $i<99999999999; $i++) {
  73.         $vatId = str_pad(++$vatIdinit, 11, 0, STR_PAD_LEFT);
  74.         ++$count;
  75.        
  76.         /* Set parameters for the request */
  77.         $params = array(
  78.             "countryCode" => "IT",
  79.             "vatNumber" => $vatId
  80.         );
  81.        
  82.         try {
  83.             /* Web service request */
  84.             $response = $client->checkVat($params);
  85.            
  86.             /* Parse response from web service */
  87.             $countryCode = $response->countryCode;
  88.             $rawvalid = $response->valid;
  89.             $valid = var_export($rawvalid,false);
  90.             $vatNumber = $response->vatNumber;
  91.             $requestDate = substr(($response->requestDate),0,10);
  92.             $name = $response->name;
  93.             $address = $response->address;
  94.  
  95.             echo " - ";
  96.  
  97.             if (!$rawvalid) {
  98.                 echo "<b style='color:#600'>".$vatNumber."</b> not found.<br /><br />\n";
  99.             }
  100.  
  101.             else {
  102.                 $match++;
  103.                 print "<b>Valid VAT number found in ".$countryCode.":</b><br />";
  104.                 print "<b><h3 style='color:#600;'>".$vatNumber."</h3></b>";
  105.                 echo $name."<br />";
  106.                 echo $address."<br />";
  107.                 echo $requestDate."<br />";
  108.                 echo "count: ".$match." matches out of ".$count."<br /><br />";
  109.                
  110.                 //add positive matches to text file
  111.                 $vatfile = "vat-".$countryCode.".txt";
  112.                 $savefile = fopen($vatfile, 'a') or die("can't open file");
  113.                
  114.                 $arrMatch = array($vatNumber, $name, $address, $requestDate);
  115.                 foreach ($arrMatch as &$data) {
  116.                     $write = $data."\n";
  117.                     fwrite($savefile, $write);
  118.                 }
  119.                 // break reference with the last element
  120.                 unset($data);
  121.                 $writeCount = "count: ".$match." matches out of ".$count."\n\n";
  122.                 fwrite($savefile, $writeCount);
  123.                 fclose($savefile);
  124.                
  125.                 print $vatNumber." data added to ".$vatfile."<br /><br />";
  126.                
  127.                 /*Launch new window with positive match result */
  128.                 echo '<script type="text/javascript" language="javascript">
  129.                 window.open("checkvat.php?id='.$vatNumber.'");
  130.                 </script>';
  131.  
  132.             }
  133.         }
  134.         catch (SoapFault $e) {
  135.             print "<b style='color:#600;'>SoapFault:</b> ";
  136.             echo print_r($e, true);
  137.             echo "<br /><br />";
  138.             print "<b style='color:#600;'>Count:</b> ";
  139.             echo print_r($count);
  140.             print "<br /><br />";
  141.         }
  142.     }
  143. }
  144.  
  145. ?>
  146. </body>
  147. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement