PDA

View Full Version : Help with modifing a script written by jscheuer1



simonf
06-05-2018, 12:57 PM
Hi

Trust you are all well.

My currency script no longer works as they changed the api on the site for the rates. I have an api with a key, but the base is set to EUR and I'm in ZAR

Your original script was

<script>jQuery(function($){function commatize(num){num=Math.ceil(num).toString(10).split("").reverse();var n=num.length,i=-1,newnum=[];while(++i<n){!(i%3)&&i&&newnum.push(",");newnum.push(num[i])}return newnum.reverse().join("")}jQuery.fn.currencyrates=function(rates){var base=this.text().replace(/\D/g,"");this.append(['<span class="usd usdsprite ocurtextsprite">$ ',commatize(base*rates.USD),'</span><br><span class="gbp gbpsprite ocurtextsprite"> ',commatize(base*rates.GBP),'</span><span class="eur eurosprite ocurtextsprite"> ',commatize(base*rates.EUR)].join(""))};$.getJSON("http://api.fixer.io/latest?base=ZAR",function(data){$(".divborder.col-md-1 h3:has(span.zar)").each(function(i,z){$(z).currencyrates(data.rates)})})});</script>

http://data.fixer.io/api/latest?access_key=55fd0ac90898af48dee7fee59d7c7de5&symbols=USD,ZAR,GBP,EUR,MXN&format=1


which gives the following:

success true
timestamp 1528194609
base "EUR"
date "2018-06-05"
rates
USD 1.16973
ZAR 14.830536
GBP 0.873718
EUR 1

So is it possible to change the script to use EUR as the base and then work out the other currencies based on the ZAR price??

Thanks in advance

Simon

Deadweight
06-06-2018, 02:15 AM
Please read their documentation:

Changing base currency
Provided changing base currency is supported on your subscription plan, you can change the currency to which your output currency rates are relative to by appending the base parameter to your API request URL and setting it to the three-letter code of your preferred base currency.

Example:


https://data.fixer.io/api/latest
? access_key = API_KEY
& base = USD

simonf
06-06-2018, 07:33 AM
Yes I can for USD10 pm lol

it give
error
code 105
type "https_access_restricted"
info "Access Restricted - Your current Subscription Plan does not support HTTPS Encryption."

http://data.fixer.io/api/latest?access_key=55fd0ac90898af48dee7fee59d7c7de5&base=ZAR&symbols=USD,ZAR,GBP,EUR,&format=1

simonf
06-06-2018, 12:45 PM
HI Guys

FYI

I figured out the sums to get it working



<script>jQuery(function($){function commatize(num){num=Math.ceil(num).toString(10).split("").reverse();var n=num.length,i=-1,newnum=[];while(++i<n){!(i%3)&&i&&newnum.push(",");newnum.push(num[i])}return newnum.reverse().join("")}jQuery.fn.currencyrates=function(rates){var base=this.text().replace(/\D/g,"");this.append(['<span class="usd usdsprite ocurtextsprite">$ ',commatize(base/rates.ZAR*rates.USD),'</span><br><span class="gbp gbpsprite ocurtextsprite"> ',commatize(base/rates.ZAR*rates.GBP),'</span><span class="eur eurosprite ocurtextsprite"> ',commatize(base/rates.ZAR)].join(""))};$.getJSON("http://data.fixer.io/api/latest?access_key=55fd0ac90898af48dee7fee59d7c7de5&symbols=USD,ZAR,GBP,EUR&format=1",function(data){$(".divborder.col-md-1 h3:has(span.zar)").each(function(i,z){$(z).currencyrates(data.rates)})})});</script>


to view live page see the site:
http://www.silvertreeestates.co.za

rgds, Simon