-
-
Save dcblogdev/8067095 to your computer and use it in GitHub Desktop.
<?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"); |
how to replace it
$get = file_get_contents("https://finance.google.com/bctzjpnsun/converter?a=$encode_amount&from=$from_Currency&to=$to_Currency");
This works for me
import urllib2
import json
curr_from = "JPY"
curr_to = "INR"
curr_input = 1
curr_pair = curr_from + "_" + curr_to
api_url = "https://free.currencyconverterapi.com/api/v5/convert?q={0}&compact=ultra".format(curr_pair)
jsonurl = urllib2.urlopen(api_url)
rate_json = json.loads(jsonurl.read())
rate = curr_input * float(rate_json[curr_pair])
print rate
As google shut down converter link you just need to replace url given below
Just replace:
https://www.google.com/finance/converter?a=
https://finance.google.com/finance/converter?a=
https://finance.google.com/bctzjpnsun/converter?a=
http://free.currencyconverterapi.com/api/v3/convert?q=
with this URL:
https://www.xe.com/currencyconverter/convert/?Amount=
https://www.xe.com/currencyconverter/convert/?Amount=
Doesn't work anymore either
Thanks for referencing free.currencyconverterapi.com , please use the "v5" onwards API. And just to give a quick update, refresh values are now updated every 60 minutes.
Friends don't worry i made the converter working again use the following code
`<?php
function convertCurrency($amount, $from, $to){
$data = file_get_contents("https://www.xe.com/currencyconverter/convert/?Amount=$amount&From=$from&To=$to");
//var_dump($data);
$doc = new DOMDocument;
$doc->loadHTML($data);
$xpath = new DOMXPath($doc);
$node = $xpath->query("//span[@class='uccResultAmount']")->item(0);
echo trim($node->nodeValue); //=> "$249.95"
}
echo convertCurrency("1", "INR", "USD");`
This is the code my application uses for the google currency api; what would be the code I should use for the free currency converter api? Nothing I've tried works.
function convert($from,$to,$amount) {
$url = "https://finance.google.com/bctzjpnsun/converter?a=1&from=" . $from . "&to=" . $to;
$page = file_get_contents($url);
preg_match_all("/bld>(.*) .*/", $page, $matches);
$value = $matches[1][0];
$response = (float) $value * $amount;
return $response;
}
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, this works like a charm. The only thing was I had to delete "/100" in the last line.
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); }
Thanks a lot @alissonlinneker.
Though I'd suggest replacing $url with:
$url = "https://www.google.com/search?q=".$from."+to+".$to;
because in some cases like BRL to CAD, Above function was throwing exception.
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); }
Need a little help here,
I used above code, tested it on my local environment and it worked smooth as butter, but once I uploaded this to my production environment, it started throwing errors, I debugged and found that Google isn't allowing this code to scrape their data. It gives "302 Moved" Error. Further explanation from Google is: "Our systems have detected unusual traffic from your computer network. Please try your request again later". So, anybody has any tip for that??
Get currency conversion .... https://pastebin.com/TFewge8t
go for it ...
public function convertCurrency()
{
$amounts = 597;
$from_currency='USD';
$to_currency='INR';
$url = 'https://www.google.co.za/search?q='.$amounts.'+' . $from_currency . '+to+' . $to_currency;
$cSession = curl_init();
curl_setopt($cSession, CURLOPT_URL, $url);
curl_setopt($cSession, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cSession, CURLOPT_SSL_VERIFYPEER, true);
$buffer = curl_exec($cSession);
curl_close($cSession);
preg_match("/<div class=\"J7UKTe\">(.*)<\/div>/",$buffer, $matches);
$matches = preg_replace("/[^0-9.]/", "", $matches[1]);
$amount = round($matches, 2);
$total = substr($amount, mb_strlen($amounts));
return number_format($total,2);
}
Hi there, there is free european exchange rates available daily
https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml
Change URL to " https://finance.google.com/finance/converter" fixed my code
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!!!!! ♥
Hello
In above all solutions USD to CNY converter not working
Can you please help me
Thanks!!
Hello
In above all solutions USD to CNY converter not workingCan you please help me
Thanks!!
Yes! Сan you give another solution?
can anyone pls help me with any free currency converter api?
Any solutions!!
It is not the best solution and not very clean, but you can do this:
- Convert from your old currency to BTC (Bitcoin)
- Convert your Bitcoin to your new currency! ;-)
Example for 100 EUR to USD:
-
100 EUR to Bitcoin. Result: 0.01282849
https://blockchain.info/tobtc?currency=EUR&value=100 -
Convert 01282849 (without "0.") to USD
https://blockchain.info/frombtc?value=01282849¤cy=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");
Here we are again :(
i created conversion using a standard Google search.
I know it isn't very clean but it works for me. Here is a link to function:
https://github.com/KhunHtetzNaing/google-currency-converter-api