Google Finance货币转换器

时间:2018-02-24 06:52:38

标签: php google-finance

我正在使用谷歌货币转换器,它适用于所有货币,但没有显示  ZAR - BTC转化的结果。

Google货币转换器代码:

<?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");

预期结果应为来自Google的8.26,但会显示消息Could not convert

4 个答案:

答案 0 :(得分:6)

我找到了一种方法来做这件事......只是将我的答案粘贴给将来需要的人。

<?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");

答案 1 :(得分:5)

当您从谷歌转换器“无法转换”收到消息时 - 这意味着转换为1 CURRENCY_A --> CURRENCY_B导致数量太少。在这种情况下,您需要进行反向转换CURRENCY_A_AMOUNT / (1 CURRENCY_B --> CURRENCY_A

答案 2 :(得分:0)

最后,我通过更新的货币转换器google网址找到了解决方案。

Click here阅读完整的解决方案,稍后再次感谢

答案 3 :(得分:0)

finance.google.com已停产尝试这些操作

intermediate_point(lat1, long1, lat2, long2, perc) {

    // Normalize percentage
    perc = perc / 100;

    // Convert to radians.
    lat1 = lat1 * Math.PI / 180;
    lat2 = lat2 * Math.PI / 180;
    long1 = long1 * Math.PI / 180;
    long2 = long2 * Math.PI / 180;

    // get angular distance between points
    var d_lat = lat2 - lat1;
    var d_lon = long2 - long1;
    var a = Math.sin(d_lat/2) * Math.sin(d_lat/2) + Math.cos(lat1) * 
        Math.cos(lat2) * Math.sin(d_lon/2) * Math.sin(d_lat/2);

    var d = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

    var A = Math.sin((1-perc)*d) / Math.sin(d);
    var B = Math.sin(perc*d) / Math.sin(d);

    var x = A * Math.cos(lat1) * Math.cos(long1) + B * cos(lat2) * cos(long2);
    var y = A * Math.cos(lat1) * Math.sin(long1) + B * cos(lat2) * sin(long2);
    var z = A * Math.sin(lat1) + B * Math.sin(lat2);

    var lat3 = Math.atan2(z, Math.sqrt(x*x + y*y));
    var long3 = Math.atan2(y, x);

    // Return result, normalising longitude to -180°...+180°
    return [long3 * 180/Math.PI, (lat3 * 180/Math.PI + 540)%360-180];
}