Author Topic: Get the most current exchange rate of any currency via online API!  (Read 3118 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
As I mentioned in a discussion post; here is code for getting the exchange rate of any currency as it currently stands! Use this function and receive the latest numbers!
To make your account, go to ExchangeRate-API.com! I won't be posting my API key here, of course.
 
exchangerate.png

Code: QB64: [Select]
  1.  
  2. PRINT "The exchange rate of USD to GBP is : "; ExchangeRate("USD", "GBP")
  3.  
  4. FUNCTION ExchangeRate$ (currency1 AS STRING, currency2 AS STRING)
  5.     DIM URL AS STRING
  6.     DIM URLFile AS STRING
  7.     DIM a%
  8.     DIM Request AS STRING
  9.     URLFile = "exchangerate"
  10.     URL = " https://v6.exchangerate-api.com/v6/YOURAPIKEYHERE/latest/" + currency1
  11.     a% = API_Request(URL, URLFile)
  12.     DIM U AS INTEGER
  13.     U = FREEFILE
  14.     OPEN URLFile FOR BINARY AS #U
  15.     IF LOF(U) <> 0 THEN
  16.         LINE INPUT #U, Request
  17.     ELSE
  18.         CLOSE #U
  19.         KILL URLFile
  20.         ExchangeRate = ""
  21.         EXIT FUNCTION
  22.     END IF
  23.     CLOSE #U
  24.     KILL URLFile
  25.     IF INSTR(Request, currency2) THEN
  26.         ExchangeRate = _TRIM$(STR$(VAL(MID$(Request, INSTR(Request, currency2) + LEN(currency2) + 2, 10))))
  27.     ELSE
  28.         ExchangeRate = "Not found"
  29.     END IF
  30.  
  31.  
  32.     FUNCTION URLDownloadToFileA (BYVAL pCaller AS LONG, szURL AS STRING, szFileName AS STRING, BYVAL dwReserved AS LONG, BYVAL lpfnCB AS LONG)
  33.  
  34. FUNCTION API_Request (URL AS STRING, File AS STRING)
  35.     API_Request = URLDownloadToFileA(0, URL + CHR$(0), File + CHR$(0), 0, 0)
« Last Edit: July 20, 2020, 06:10:50 pm by SpriggsySpriggs »
Shuwatch!