Hi friends

i am working for an shopping cart using php script, i placed a drop down option to display currency rate as per user selection(INR and USD). By default it will display all the price in INR, if any user select USD, then it has to show all the values in USD.I dont know how to display the USD live rates. can any one please help to resove the issue.

Dani AI

Generated

Good call by @diafol and on using a proper FX API. The old Yahoo CSV endpoint shown later in the thread was shut down back in 2017, which is why and saw that error. Today (Sept 2025), an easy, no-key option is the Frankfurter API, which publishes daily ECB reference rates; they refresh around 16:00 CET on working days. If you need more frequent updates or SLAs, consider commercial APIs such as exchangerate.host (requires an access key). These two changes alone will make your cart much more reliable. Frankfurter docs. ECB reference rates. exchangerate.host documentation. For background on the Yahoo shutdown, see this Stack Overflow thread noting Yahoo’s discontinuation. Stack Overflow confirmation.

Drop-in PHP example using Frankfurter and simple server-side caching (avoid calling the API on every page/product):

function convertCurrency($amount, $from = 'INR', $to = 'USD') {
    $cacheDir = __DIR__ . '/cache';
    if (!is_dir($cacheDir)) mkdir($cacheDir, 0775, true);
    $cacheFile = "$cacheDir/fx-{$from}-{$to}.json";
    $ttl = 3600; // 1 hour

    if (file_exists($cacheFile) && (time() - filemtime($cacheFile) < $ttl)) {
        $json = file_get_contents($cacheFile);
    } else {
        $url = 'https://api.frankfurter.app/latest?from=' . rawurlencode($from) . '&to=' . rawurlencode($to);
        $ch = curl_init($url);
        curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 5]);
        $json = curl_exec($ch);
        $ok = curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200;
        curl_close($ch);
        if ($ok) file_put_contents($cacheFile, $json);
        elseif (file_exists($cacheFile)) $json = file_get_contents($cacheFile);
        else throw new RuntimeException('FX rate unavailable');
    }

    $data = json_decode($json, true);
    if (!isset($data['rates'][$to])) throw new RuntimeException('Rate missing');
    return round($amount * (float)$data['rates'][$to], 2);
}
// Example: display USD for INR-priced catalog
$usd = convertCurrency(1500, 'INR', 'USD');

Practical tips: keep your catalog priced in one base currency (INR here) and convert only for display; cache rates (15–60 min) and fix the rate at checkout to avoid price jumps; use proper currency formatting and rounding rules per currency. The ECB rates are end-of-day references, so caching aligns well with their update schedule. ECB timing note.

Recommended Answers

All 8 Replies

Member Avatar for Member #120589

You could use a site like this - which has an API. I assume that you'll need an API of some description.

https://openexchangerates.org/

You could get a free account or depending on your traffic, pay something like $12/mo.

Thanks diafol, i will check it

You'll need to use an exchange rate API, then parse/decode the results. Google and Yahoo each have APIs (a quick search should turn up more info), and another resource I found is https://openexchangerates.org/

EDIT: Just saw diafol's response. Sorry, had this tab open for a while before I got to it.

HI Friends

Thanks for your post, with help of your post i got an idea and i used the below free API code

<?php
        $from   = 'USD'; /*change it to your required currencies */
        $to     = 'INR';
        $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 */
        $dollarValue = $allData[1];

        echo 'Value of $1 in Indian Rupees is &#x20B9; '.$dollarValue;
?>

Thank You. Its Working, I have little change on it....

<?php

$_POST['amount']="35.00";
$_POST['from']="INR";
$_POST['to']="USD";

$amount = urlencode($_POST['amount']);
$from_Currency = urlencode($_POST['from']);
$to_Currency = urlencode($_POST['to']);

$url = 'http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s='. $from_Currency . $to_Currency .'=X';
$handle = @fopen($url, 'r');
if ($handle) 
{
    $result = fgets($handle, 4096);
    fclose($handle);

    $allData = explode(',',$result); /* Get all the contents to an array */
    $dollarValue = $allData[1]*$amount;
    echo  round($dollarValue , 2);
}
else{
    echo "ERROR";
}

?>

I got error "It has come to our attention that this service is being used in violation of the Yahoo Terms of Service. As such, the service is being discontinued. For all future markets and equities data research, please refer to finance.yahoo.com." after 2-3 weaks of using this code.
Please advice some another working yahoo api.

Not working now ...... Yahoo shutdown this service.

If any other way to get value then please let me know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.