Link to home
Start Free TrialLog in
Avatar of Graeme McGilvray
Graeme McGilvrayFlag for Australia

asked on

currencylayer API intergrate to ASP

Hi all, I am new to this, so as much break down as possible would be extremely appreciated

I have got this currencylayer API:
http://apilayer.net/api/live?access_key=accesskey¤cies=AUD&source=USD&format=1

the outcome, is what I want to put into ASP to do some converting

Thanks in advance
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

the output of that API is a json string. what are you trying to do with your ASP code or what you trying to display from this output?
Avatar of Graeme McGilvray

ASKER

I would like the USD to AUD rate (which currently is 1.351717) and use that to auto update my pricing as i buy from overseas in foreign currency
the json string returned an invalid output.
{
  "success":false,
  "error":{
    "code":101,
    "type":"invalid_access_key",
    "info":"You have not supplied a valid API Access Key. [Technical Support: support@apilayer.com]"
  }
}

Open in new window


can you provide a valid request here?
I can, but that would mean giving out my access key, which is private

This is the output thou:
{
  "success":true,
  "terms":"https:\/\/currencylayer.com\/terms",
  "privacy":"https:\/\/currencylayer.com\/privacy",
  "timestamp":1467120967,
  "source":"USD",
  "quotes":{
    "USDAUD":1.351717
  }
}

Open in new window

Anything Ryan?
Hi Good day,

You can look at aspjson for handling JSON with VBScript

http://code.google.com/p/aspjson/

You can also use Javascript as your classic asp server side scripting language, which would involve you rewriting your server http request in Javascript, but it would make the json part of the page much easier.

You can even use VBS and JS in the same page, eg

<%@ Language=javascript %>

<script language="VBScript" runat="server">
Set xmlhttp = CreateObject("Msxml2.ServerXMLHTTP.6.0")
xmlhttp.open "GET", "http://someip:8080/Publisher/Titles/Paging/0,0,tc?output=json", 0
xmlhttp.send ""
Response.AddHeader "Content-Type", "application/json;charset=UTF-8"
Response.Charset = "UTF-8"
pageReturn = xmlhttp.responseText    
Set xmlhttp = Nothing    
</script>

<% var resultcount = pageReturn.Titles.resultCount;
   var moreresources = pageReturn.Titles.moreResources;
%>


<html>
<body>

<%=resultcount%>, <%=moreresources%>
</body>
</html>
Hi Charles and thank you very much for this. As I said I am very new to APIs, unfortunately i dont understand much of it.

the outcome I am looking for is:

'1.351717' - I want to put this into something I can use eg CurrConvert='TheAPIOutput'

I tried to copy and paste the code you have suggested and replace the string with my API

However it is coming up with an error:

Microsoft VBScript compilation error '800a0401'

Expected end of statement

var resultcount = pageReturn.Titles.resultCount;
-----------------------------------------------^

Open in new window

Charles?
sorry for being late to reply.

knowing that you wish to get the value of "USDAUD" from the return string.

just another question before we proceed, do you want to do it in ASP or JavaScript? reason being it's quite easy to read a JSON string from JavaScript.
Hi Ryan, as long as I can use the out in my ASP querys, it wouldnt matter (however just saying that, prob ASP)

Chhers
you probably can try this:
<%

  Dim oXMLHTTP
  Dim strStatusTest

  Set oXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP")

  url = "http://apilayer.net/api/live?access_key=accesskey&currencies=AUD&source=USD&format=1"

  CurrValue = 0
  
  oXMLHTTP.Open "POST", url, False
  oXMLHTTP.Send
  
  If oXMLHTTP.Status = 200 Then

    GetTextFromUrl = oXMLHTTP.responseText
    Arr = Split(GetTextFromUrl, vbCrLf)
    for i = 0 to ubound(Arr)
        if instr(Arr(i), "USDAUD") > 0 then
            CurrValue = CDbl(Split(Arr(i),":")(1))
            exit for
        end if
    next
  End If

  response.Write("GetTextFromUrl = " & GetTextFromUrl & "<br><br>CurrValue = " & CurrValue)
%>

Open in new window

you may change the URL to put in the valid parameters
and change the method from POST to GET if necessary.
Hi Ryan and thanks for that, have replaced the accesskey and for this msg:

Microsoft VBScript runtime error '800a000d'
Type mismatch: 'CDbl'

CurrValue = CDbl(Split(Arr(i),":")(1))

Open in new window

maybe you can just use:
CurrValue = Split(Arr(i),":")(1)

Open in new window


the code I provided has been tested before I posted.
Hi Ryan, have just changed it, seems to have moved onto something else now

Microsoft VBScript runtime error '800a000d'
Type mismatch: '[string: "true, "terms""]'    <-- coming from the API by the looks
CurrValue = Split(Arr(i),":")(1)

Open in new window

so it seems that there's other errors which making your ASP not working properly.

can you tell us more about your server info? such as Windows/ IIS ver etc.

in your IIS do you configured to use VBScript as the script language?

User generated image
User generated imageI have Windows 2003 SBS / IIS6

I dont have the IIS7 Manager (above) unfortunately
unfortunately i don't have IIS6 for troubleshooting.

maybe you can try add this at the beginning of your scripts and see if that resolved your error?

<%@ LANGUAGE="VBScript"%>

Open in new window

Hi Ryan, I have just added that, same error
can we know what's the value of GetTextFromUrl if you response it out?
response.write(GetTextFromUrl)
response.end

Open in new window

try view and copy paste the output here?

and can you run any other asp pages in your machine?
All other ASP pages run perfectly fine on my site (and alot of them)

unable to get a response out of GetTextFromUrl because the code errors before it get to its line
can you try run this and what you get from here?
<%

  Dim oXMLHTTP
  Dim strStatusTest

  Set oXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP")

  url = "http://apilayer.net/api/live?access_key=accesskey&currencies=AUD&source=USD&format=1"
  
  oXMLHTTP.Open "POST", url, False
  oXMLHTTP.Send
  
  If oXMLHTTP.Status = 200 Then

    GetTextFromUrl = oXMLHTTP.responseText
    response.Write(GetTextFromUrl)
    response.End

  End If
%>

Open in new window

Hi Ryan

here is the output:

{ "success":true, "terms":"https:\/\/currencylayer.com\/terms", "privacy":"https:\/\/currencylayer.com\/privacy", "timestamp":1468289826, "source":"USD", "quotes":{ "USDAUD":1.320981 } }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think we have a winner! :)

GetTextFromUrl = { "success":true, "terms":"https:\/\/currencylayer.com\/terms", "privacy":"https:\/\/currencylayer.com\/privacy", "timestamp":1468293132, "source":"USD", "quotes":{ "USDAUD":1.318202 } }

CurrValue = 1.318202

Open in new window

coool, eventually we make it right!