如何通过 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",因此一种基于 space 划分数据的简单解决方案文字对我不起作用。
正如您所说的那样,邮政编码将采用 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);
我正在使用 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",因此一种基于 space 划分数据的简单解决方案文字对我不起作用。
正如您所说的那样,邮政编码将采用 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);