Syed Saqlain
Syed Saqlain

Reputation: 584

Google Finance Currency Converter

I am working on google currency converter and it's working fine for all currencies but not showing results of ZAR - BTC conversion.

Google currency converter code :

<?php
function convertCurrency($amount, $from, $to){
    $data = file_get_contents("https://finance.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("1000000", "ZAR", "BTC");

The expected result should be 8.26 from google but it shows message Could not convert

Upvotes: 11

Views: 3138

Answers (3)

But Jao
But Jao

Reputation: 21

finance.google.com has been discontinued, try these:

// google API - Load time: 558 ms
function google_money_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);
    $finalData=str_replace(',', '.', $finalData);
    return (float)$finalData[1]*$amount;
}


// free.currencyconverter API - Load time: 95ms
function money_convert($from, $to, $amount)
{
    $url = "http://free.currencyconverterapi.com/api/v5/convert?q=$query&compact=ultra";
    $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);
    $response = json_decode($response, true);
    $responseOld=$response;
    // print_r($response);
    return $response[$query]*$amount;
}

Upvotes: 0

Agnius Vasiliauskas
Agnius Vasiliauskas

Reputation: 11277

When you get message from google converter "Could not convert" - it means that conversion
1 CURRENCY_A --> CURRENCY_B results in too small amount. In this case you need to make reverse conversion CURRENCY_A_AMOUNT / (1 CURRENCY_B --> CURRENCY_A)

Upvotes: 5

Syed Saqlain
Syed Saqlain

Reputation: 584

I have found a way to do thiss.. just pasting my answer for someone who needed in future.

<?php
function convertCurrency($amount, $from, $to){
    $data = file_get_contents("https://finance.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);
}
 convertCurrency("1", "BTC", "ZAR");



function ZARtoBTC($amount){
      $BTC = convertCurrency("1", "BTC", "ZAR");
       $f_amount = number_format($amount, 3);

        $val = $f_amount / $BTC ;

       return  number_format($val, 2);
}
echo ZARtoBTC("100000");

Upvotes: 5

Related Questions