MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/web_dev/comments/32rjlw/question_about_converting_usd_to_eur_dynamically/
r/web_dev • u/imjp • Apr 16 '15
I'm working on a real estate website for a client which lists his properties in USD and Euros.
Is there a way to automatically change the value of the Euro on the website using some kind of javascript or php?
8 comments sorted by
3
function USDtoEURO($usd) { //You may want to cache this curl. $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); $xmlResponse = curl_exec($ch); curl_close($ch); $xml = simplexml_load_string($xmlResponse); $json = json_encode($xml); $array = json_decode($json,TRUE); foreach($array['Cube']['Cube']['Cube'] as $entry){ if($entry['@attributes']['currency'] == 'USD'){ return $usd / $entry['@attributes']['rate']; } } //If USD not in list return false; } echo USDtoEURO(2000);
1 u/imjp Apr 16 '15 Thanks man, I'll give that a try and get back to you. 1 u/zapplebee-lives Apr 16 '15 I made a slight change, I accidentally flipped the conversion.return $entry['@attributes']['rate'] * $usd;toreturn $usd / $entry['@attributes']['rate']; 1 u/imjp Apr 16 '15 hey man, the first code you sent me already works. Shoudl I still change anything? 1 u/imjp Apr 16 '15 Thanks man :) 1 u/zapplebee-lives Apr 16 '15 Yeah, change it or else you're converting Euros to USD instead of the other way around.
1
Thanks man, I'll give that a try and get back to you.
1 u/zapplebee-lives Apr 16 '15 I made a slight change, I accidentally flipped the conversion.return $entry['@attributes']['rate'] * $usd;toreturn $usd / $entry['@attributes']['rate']; 1 u/imjp Apr 16 '15 hey man, the first code you sent me already works. Shoudl I still change anything? 1 u/imjp Apr 16 '15 Thanks man :) 1 u/zapplebee-lives Apr 16 '15 Yeah, change it or else you're converting Euros to USD instead of the other way around.
I made a slight change, I accidentally flipped the conversion.
return $entry['@attributes']['rate'] * $usd;
to
return $usd / $entry['@attributes']['rate'];
1 u/imjp Apr 16 '15 hey man, the first code you sent me already works. Shoudl I still change anything? 1 u/imjp Apr 16 '15 Thanks man :) 1 u/zapplebee-lives Apr 16 '15 Yeah, change it or else you're converting Euros to USD instead of the other way around.
hey man, the first code you sent me already works. Shoudl I still change anything?
1 u/imjp Apr 16 '15 Thanks man :) 1 u/zapplebee-lives Apr 16 '15 Yeah, change it or else you're converting Euros to USD instead of the other way around.
Thanks man :)
Yeah, change it or else you're converting Euros to USD instead of the other way around.
http://php.net/manual/en/locale.acceptfromhttp.php
This would probably suit your needs?
3
u/zapplebee-lives Apr 16 '15 edited Apr 16 '15
1
u/PrinceJonn Apr 16 '15
http://php.net/manual/en/locale.acceptfromhttp.php
This would probably suit your needs?