You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用R对接VIES接口实现客户VAT税号有效性验证的问题咨询

R语言调用VIES平台实现VAT税号核验的正确方案

你之前直接调用网页查询接口的方式会失效,是因为该接口仅面向浏览器端访问,服务端有会话、请求来源等校验规则,未满足条件时就会返回异常内容。使用官方提供的SOAP API是最稳定的实现方式,具体代码如下:

依赖准备

首先安装并加载所需的两个核心包:

install.packages(c("httr", "xml2"))
library(httr)
library(xml2)

封装核验函数

我们可以将查询逻辑封装为可复用的函数,输入待核验的国家代码、VAT税号即可返回核验结果:

check_vat <- function(country_code, vat_number) {
  # 构造SOAP请求体
  soap_body <- '<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:ec.europa.eu:taxud:vies:services:checkVat:types">
   <soapenv:Header/>
   <soapenv:Body>
      <urn:checkVat>
         <urn:countryCode>%s</urn:countryCode>
         <urn:vatNumber>%s</urn:vatNumber>
      </urn:checkVat>
   </soapenv:Body>
</soapenv:Envelope>'
  # 填充参数到请求体
  formatted_body <- sprintf(soap_body, toupper(country_code), gsub("\\s", "", vat_number))
  
  # 发起POST请求
  response <- POST(
    url = "https://ec.europa.eu/taxation_customs/vies/services/checkVatService",
    add_headers(
      "Content-Type" = "text/xml;charset=utf-8",
      "SOAPAction" = ""
    ),
    body = formatted_body
  )
  
  # 解析返回的XML结果
  if (http_status(response)$category == "Success") {
    res_xml <- read_xml(content(response, "text"))
    # 提取返回字段
    ns <- xml_ns(res_xml)
    result <- list(
      valid = as.logical(xml_text(xml_find_first(res_xml, "//:valid", ns))),
      country_code = xml_text(xml_find_first(res_xml, "//:countryCode", ns)),
      vat_number = xml_text(xml_find_first(res_xml, "//:vatNumber", ns)),
      company_name = xml_text(xml_find_first(res_xml, "//:name", ns)),
      company_address = xml_text(xml_find_first(res_xml, "//:address", ns)),
      request_date = xml_text(xml_find_first(res_xml, "//:requestDate", ns))
    )
    return(result)
  } else {
    stop(sprintf("请求失败,状态码:%s", status_code(response)))
  }
}

测试示例

你可以用以下代码测试法国VAT税号的核验效果:

# 测试示例:法国税号核验
test_res <- check_vat(country_code = "FR", vat_number = "23489967794")
print(test_res)

注意事项

  • VIES平台对请求频率有一定限制,请勿短时间内发起大量批量查询请求,避免IP被临时封禁
  • 部分欧盟国家返回的工商信息可能存在空值,属于正常情况,是对应国家的数据源未同步全量信息
  • 输入VAT税号时不需要带国家代码前缀,函数会自动处理传入的国家代码参数

内容的提问来源于stack exchange,提问作者gaut

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 18:36:02