Jump to content

Multi Currency using rates from yahoo API


jonnystudent

Recommended Posts

Hello all, I'm looking for some assistance with some code.  I'm trying to put this to bed as it's driving me nuts.  The Psuedo code for this would be to

 

1) Download the rates from the Yahoo API and declare my base rate to my foreign exchange.

2) Store an array of currencies to choose from to compare against the base currency.

3) I then GET the chosen currency and register it in a SESSION to be used on other pages. (I'm unsure if this is correct?)

4) I then calculate the Price of my product which is in GBP to the selected foreign currency.

5) Output the converted price anywhere on the page.

 

My script takes GET values from the URL like so..

 

.com?c=EUR

 

My code is like so..

 

1) First I get the rates from the Yahoo API

session_start();

$from   = 'GBP';
$to     = '$c';
$url = 'http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s='. $from . $to .'=X';
$handle = @fopen($url, 'r');

if ($handle) {
    $result = fgets($handle, 4096);
    fclose($handle);
}
$allData = explode(',',$result); /* Get all the contents to an array */
$PoundValue = $allData[1];

 

2) Then I store an array of the currencies.

$currency_array = array ('USD','EUR','RMB','JPY','AUD','CHF')

 

3) Then I get the chosen currency.

if(isset($_GET['c'])) {  
$c = $_GET['c'];

    if(array($currency_array)) {
        $_SESSION['currency_array'] = $c;
    }
}

 

4) I then calculate the product price.

$Total = $Price * $currency_array; 

$outprice = number_format($Total, 2, '.', ',');

 

5) Then I output on the page

<?php echo .$outprice; ?>

 

So is all my coding logically in the right order?

 

Any help would be greatly appreciated.  Thank you.

Link to comment
Share on other sites

Just as a side note.  When I type in the URL ending in .php?c=EUR

 

I have put a dump in there and my output is

 

array

  0 => string '"GBP$C=X"' (length=9)

  1 => string '0.00' (length=4)

  2 => string '"N/A"' (length=5)

  3 => string '"N/A"

 

' (length=7)

 

Why is the $c value not being returned as the chosen currency?

 

Cheers,

 

Jonah

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.