Skip to content

Instantly share code, notes, and snippets.

@dcblogdev
Created December 21, 2013 09:04
Show Gist options
  • Save dcblogdev/8067095 to your computer and use it in GitHub Desktop.
Save dcblogdev/8067095 to your computer and use it in GitHub Desktop.
Use Google finance calculator to convert currency with php
<?php
function convertCurrency($amount, $from, $to){
$data = file_get_contents("https://www.google.com/finance/converter?a=$amount&from=$from&to=$to");
preg_match("/<span class=bld>(.*)<\/span>/",$data, $converted);
$converted = preg_replace("/[^0-9.]/", "", $converted[1]);
return number_format(round($converted, 3),2);
}
echo convertCurrency("10.00", "GBP", "USD");
@AyaAhmedEliwa
Copy link

Change URL to " https://finance.google.com/finance/converter" fixed my code

@Devon002
Copy link

@graphgear1000

Fixed:

function convert($from,$to,$amount){
    $url = "https://www.google.com/search?q=".$from.$to;
    $request = curl_init();
    $timeOut = 0;
    curl_setopt ($request, CURLOPT_URL, $url);
    curl_setopt ($request, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($request, CURLOPT_USERAGENT,"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36");
    curl_setopt ($request, CURLOPT_CONNECTTIMEOUT, $timeOut);
    $response = curl_exec($request);
    curl_close($request);

    preg_match('~<span [^>]* id="knowledge-currency__tgt-amount"[^>]*>(.*?)</span>~si', $response, $finalData);
    return floatval((floatval(preg_replace("/[^-0-9\.]/","", $finalData[1]))/100) * $amount);
}

Thank you, Alisson!!!!! ♥

@gautamdrc
Copy link

Hello
In above all solutions USD to CNY converter not working

Can you please help me

Thanks!!

@snznaota
Copy link

Hello
In above all solutions USD to CNY converter not working

Can you please help me

Thanks!!

Yes! Сan you give another solution?

@horlard
Copy link

horlard commented Aug 8, 2019

can anyone pls help me with any free currency converter api?

@Govindarajn
Copy link

Any solutions!!

@Navidiscounter
Copy link

Navidiscounter commented Nov 15, 2019

It is not the best solution and not very clean, but you can do this:

  1. Convert from your old currency to BTC (Bitcoin)
  2. Convert your Bitcoin to your new currency! ;-)

Example for 100 EUR to USD:

  1. 100 EUR to Bitcoin. Result: 0.01282849
    https://blockchain.info/tobtc?currency=EUR&value=100

  2. Convert 01282849 (without "0.") to USD
    https://blockchain.info/frombtc?value=01282849&currency=USD

Attention: In this solution you can convert max 1 full Bitcoin (~8000 EUR)
PHP Code:

function convert($amount, $from, $to)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://blockchain.info/tobtc?currency=" . $from . "&value=" . $amount);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$conversion = curl_exec($ch);
$conversion = substr($conversion, 2);
curl_setopt($ch, CURLOPT_URL, "https://blockchain.info/frombtc?currency=" . $to . "&value=" . $conversion);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
return $conversion = curl_exec($ch);
}
echo convert(100, "EUR", "USD");

@mokoshalb
Copy link

Here we are again :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment