Search code examples
phpapicodeignitergoogle-finance

Google finance Currency converter not working


My Code for currecy converter using google finance is not working

$amount="10";
      $from="USD";
      $to="INR";
    $data = file_get_contents("https://finance.google.com/bctzjpnsun/converter?a=$amount&from=$from&to=$to");
    preg_match("/<span class=bld>(.*)<\/span>/",$data, $converted);
    $converted = preg_replace("/[^0-9.]/", "", $converted[1]);
    echo number_format(round($converted, 3),2); exit;

I am getting the result as 0.00.

Is there any alternative method other than this?


Solution

  • $amount="10";
    $from="USD";
    $to="INR";
    $url = file_get_contents('https://free.currencyconverterapi.com/api/v5/convert?q=' . $from . '_' . $to . '&compact=ultra');
    $json = json_decode($url, true);
    $rate = implode(" ",$json);
    $total = $rate * $amount;
    $rounded = round($total); 
    return $rounded;
    

    use this code