2015-09-24 70 views
3

我试图在Microsoft Access 2013中创建实时链接,以提供基于REST的API(this API, to be specific)提供的数据。最终目标是数据在查询中可用,就好像它是本地数据库一样。创建与基于REST的API的Microsoft Access数据库连接

这是如何实现的?具体来说,我正在努力如何让Access根据请求调用API。我认为实现类似结果的唯一方法是编写一个脚本,通过API抽取整个数据库,并将其转换为Access可读格式,然后按设定的时间间隔运行该脚本。但我真的很想找到一个实时工作的解决方案,即使它比本地缓存数据库要慢。

回答

7

由于对RESTful Web服务的调用实际上只是一种特定类型的HTTP请求,您至少可以使用Microsoft XML库向Web服务发送HTTP请求并解析返回的任何内容。例如,当我运行下面的VBA代码

' VBA project Reference required: 
' Microsoft XML, v3.0 

Dim httpReq As New MSXML2.ServerXMLHTTP 
httpReq.Open "GET", "http://whois.arin.net/rest/poc/KOSTE-ARIN", False 
httpReq.send 
Dim response As String 
response = httpReq.responseText 
Debug.Print response 

字符串变量response包含了我的请求,XML响应。它看起来像这样(为重新格式化后的可读性):

<?xml version='1.0'?> 
<?xml-stylesheet type='text/xsl' href='http://whois.arin.net/xsl/website.xsl' ?> 
<poc xmlns="http://www.arin.net/whoisrws/core/v1" xmlns:ns2="http://www.arin.net/whoisrws/rdns/v1" 
xmlns:ns3="http://www.arin.net/whoisrws/netref/v2" termsOfUse="https://www.arin.net/whois_tou.html" 
inaccuracyReportUrl="http://www.arin.net/public/whoisinaccuracy/index.xhtml"> 
    <registrationDate>2009-10-02T11:54:45-04:00</registrationDate> 
    <ref>http://whois.arin.net/rest/poc/KOSTE-ARIN</ref> 
    <city>Chantilly</city> 
    <companyName>ARIN</companyName> 
    <iso3166-1> 
    <code2>US</code2> 
    <code3>USA</code3> 
    <name>UNITED STATES</name> 
    <e164>1</e164> 
    </iso3166-1> 
    <firstName>Mark</firstName> 
    <handle>KOSTE-ARIN</handle> 
    <lastName>Kosters</lastName> 
    <emails> 
    <email>ma[email protected]</email> 
    <email>[email protected]</email> 
    </emails> 
    <resources termsOfUse="https://www.arin.net/whois_tou.html" 
    inaccuracyReportUrl="http://www.arin.net/public/whoisinaccuracy/index.xhtml"> 
    <limitExceeded limit="256">false</limitExceeded> 
    </resources> 
    <phones> 
    <phone> 
     <number>+ 1-703-227-9870</number> 
     <type> 
     <description>Office</description> 
     <code>O</code> 
     </type> 
    </phone> 
    </phones> 
    <postalCode>20151</postalCode> 
    <comment> 
    <line number="0">I&#39;m really MAK21-ARIN</line> 
    </comment> 
    <iso3166-2>VA</iso3166-2> 
    <streetAddress> 
    <line number="0">3635 Concorde Parkway</line> 
    </streetAddress> 
    <updateDate>2015-05-26T11:36:55-04:00</updateDate> 
</poc> 

什么得到由Web服务返回的可能看起来有些不同。或者,正如上述ARIN whois RWS的情况一样,您可能有几种数据格式供您选择; XML只是默认值。我可以要求使用

httpReq.Open "GET", "http://whois.arin.net/rest/poc/KOSTE-ARIN.txt", False 

一个纯文本的反应在这种情况下response将包含

# 
# ARIN WHOIS data and services are subject to the Terms of Use 
# available at: https://www.arin.net/whois_tou.html 
# 


Name:   Kosters, Mark 
Handle:   KOSTE-ARIN 
Company:  ARIN 
Address:  3635 Concorde Parkway 
City:   Chantilly 
StateProv:  VA 
PostalCode:  20151 
Country:  US 
RegDate:  2009-10-02 
Updated:  2015-05-26 
Comment:  I'm really MAK21-ARIN 
Phone:   +1-703-227-9870 (Office) 
Email:   [email protected] 
Email:   [email protected] 
Ref:   http://whois.arin.net/rest/poc/KOSTE-ARIN 
# 
# ARIN WHOIS data and services are subject to the Terms of Use 
# available at: https://www.arin.net/whois_tou.html 
# 
相关问题