【问题标题】:Google Finance Currency Converter谷歌金融货币转换器
【发布时间】:2018-02-24 06:52:37
【问题描述】:

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

谷歌货币转换器代码:

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

预期的结果应该是来自谷歌的8.26,但它显示消息Could not convert

【问题讨论】:

  • 谷歌链接对比特币的几种不同货币说了同样的话。编程问题如何?你没有用网络浏览器尝试链接吗?
  • 我投票决定将此问题作为题外话结束,因为 SO 无法告诉您为什么您会获得网页的某些结果
  • 先检查我的答案。
  • 这不是由于网络浏览器#James..
  • 我无法先检查您之后发布的内容,但仍然与编程无关。

标签: php google-finance


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    当您收到来自 google 转换器的消息 “无法转换” - 这意味着转换
    1 CURRENCY_A --&gt; CURRENCY_B 导致的金额太小。这种情况下需要进行反向转换CURRENCY_A_AMOUNT / (1 CURRENCY_B --&gt; CURRENCY_A)

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        finance.google.com 已停用试试这些

        // 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;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-11-07
          • 2020-08-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-27
          相关资源
          最近更新 更多