2015-09-24 113 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 
# 
相關問題