如何通过SOAP从VIES数据库中收集公司数据


How to gather company data from VIES database via SOAP

我正在使用VIES数据库来收集公司数据,基于我的PHP应用程序的欧洲增值税编号。

我需要的东西是:

  • 城市
  • 街道名称
  • 门牌号码
  • 邮政编码
  • 公司名称

作为单独的数据,但VIES数据库将所有数据作为一个字符串提供给我。

工作示例:

<?php
try {
    $opts = array(
        'http' => array(
            'user_agent' => 'PHPSoapClient'
        )
    );
    $context = stream_context_create($opts);
    $client = new SoapClient(
        'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl',
        array('stream_context' => $context,
              'cache_wsdl'     => WSDL_CACHE_NONE)
    );
    $result = $client->checkVat(
        array(
            'countryCode' => 'PL',
            'vatNumber'   => '5242106963'
        )
    );
    print_r($result);
} catch (Exception $e) {
    echo $e->getMessage();
}
?>

我正在接收:

stdClass Object (
    [countryCode] => PL
    [vatNumber] => 5242106963
    [requestDate] => 2015-02-20+01:00
    [valid] => 1
    [name] => COCA-COLA HBC POLSKA SPÓŁKA Z OGRANICZONĄ ODPOWIEDZIALNOŚCIĄ
    [address] => ANNOPOL 20 03-236 WARSZAWA
)

但我需要这样的地址:

$street='ANNOPOL';
$number='20';
$city='WARSZAWA';
$postcode='03-236';

此外,请记住,对于其他公司来说,街道名称或城市可以有多个单词,如"New York",因此基于单词之间的空格划分数据的简单解决方案对我来说不起作用。

正如您所说,邮政编码将采用99-999格式,并且假设街道编号(+任何平面标识)始终以数字开头,您可以使用preg_match来解析地址字符串:

$result = new stdClass();
$result->address = 'Wita Stwosza 15 M5 31-042 Kraków';
preg_match(
    '#'
    . '(.*?)'             // Match as many chars as possible (street)
    . ''s+('d+(?:.*?))'   // Space, then number and possibly flat (number)
    . ''s+('d'd'-'d'd'd)' // Space, then digits/dash/digits (postcode)
    . ''s(.*)'            // Space, then everything after that (city)
    . '#',
    $result->address,
    $matches
);
list ($street, $number, $postcode, $city) = array_slice($matches, 1);
echo "Street:   $street", PHP_EOL;
echo "Number:   $number", PHP_EOL;
echo "Postcode: $postcode", PHP_EOL;
echo "City:     $city", PHP_EOL;

输出:

Street:   Wita Stwosza
Number:   15 M5
Postcode: 31-042
City:     Kraków

据我所见,VIES数据已经在结果中内置了换行符。因此,您应该能够根据换行符进行分解。然后,这将只是一个计算邮政编码是最后一个还是城市的例子。

为了证实我所说的:

echo nl2br($result->address);