【问题标题】:GOOGLE Finance converter api is not respondingGOOGLE 财务转换器 api 没有响应
【发布时间】:2018-02-24 15:38:35
【问题描述】:

我一直在尝试使用 PHP 中的谷歌金融转换器转换货币。

我使用了以下代码。

$amount = 100;
$from_Currency = "INR";
$to_Currency = "BTC";

 $amount = urlencode($amount);
  $from_Currency = urlencode($from_Currency);
  $to_Currency = urlencode($to_Currency);

$get = file_get_contents("https://finance.google.com/finance/converter?a=$amount&from=$from_Currency&to=$to_Currency&meta=ei%3DZsa7WeGkE4_RuASY95SQAw");

  $get = explode("<span class=bld>",$get);


  $get = explode("</span>",$get[1]);  

  $converted_amount = preg_replace("/[^0-9\.]/", null, $get[0]);
  echo ceil($converted_amount);
 ?>

但我收到以下错误

Warning: file_get_contents(https://finance.google.com/finance/converter?a=100&from=INR&to=BTC&meta=ei%3DZsa7WeGkE4_RuASY95SQAw): failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in F:\Xampp\htdocs\test\index.php on line 11

Notice: Undefined offset: 1 in F:\Xampp\htdocs\test\index.php on line 16
0

如何解决这个错误?

【问题讨论】:

  • 您在定义$get 时缺少一个值。我已经用精炼的代码发布了我的答案。
  • Google 金融转换器 API 不适用于某些货币代码。参考这个:finance.google.com/finance/converter比特币在这里也不起作用。所以使用php函数来转换它会很有用。
  • 是的@sucharithagidla,我们必须使用finance.google.com,我也在使用它。我的代码也适用于 USD 到 BTC 的转换,但是当我尝试将 INR 转换为 BTC 时会出现该错误
  • 昨天我们遇到了错误。让我知道解决方案

标签: php converter google-finance google-finance-api


【解决方案1】:

当我的脚本出错但由于某种原因转换错误时,上述方法对我有用。看来我需要做的就是更新脚本中的 URL;我现在有以下内容(我一次只转换一种货币,但你应该能够弄清楚如何适应!):

function convertCurrency($to){
    $url = "http://finance.google.com/finance/converter?a=1&from=GBP&to=$to";
    // Previously: $url = "http://www.google.com/finance/converter?a=1&from=GBP&to=$to";
    $request = curl_init();
    $timeOut = 0;
    curl_setopt ($request, CURLOPT_URL, $url);
    curl_setopt ($request, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($request, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
    curl_setopt ($request, CURLOPT_CONNECTTIMEOUT, $timeOut);
    $response = curl_exec($request);
    curl_close($request);

    $regularExpression = '#\<span class=bld\>(.+?)\<\/span\>#s';
    preg_match($regularExpression, $response, $finalData);
    $rate = $finalData[0];
    $rate = strip_tags($rate);
    $rate = substr($rate, 0, -4);
    return $rate;
}

我希望这会有所帮助。
G

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。我认为谷歌已经改变了输出结果的方式。 试试这个(对我有用,今天下午 12.14 CEST (UTC+2) 测试)

      function convertCurrency($amount, $from, $to) {
         $url = 'http://finance.google.com/finance/converter?a=' . $amount . '&from=' . $from . '&to=' . $to;
         $data = file_get_contents($url);
             preg_match_all("/<span class=bld>(.*)<\/span>/", $data, $converted);
             $final = preg_replace("/[^0-9.]/", "", $converted[1][0]);
            return round($final, 3);
       }
    
       echo convertCurrency(1, 'EUR', 'USD');  // output: 1.195 
    
    /* I got errors until i've changed this line:
       $final = preg_replace("/[^0-9.]/", "", $converted[1]); to:
       $final = preg_replace("/[^0-9.]/", "", $converted[1][0]);
       .. maybe it works for your code too
    */
    

    【讨论】:

    • 美元和欧元之间的转换在我的 sn-p 中也可以正常工作。但它不会将 INR 转换为 BTC,反之亦然
    【解决方案3】:

    试试这个
    为我工作。
    将其更改为:

    $url = "https://finance.google.com/bctzjpnsun/converter?a=$amount&from=$from_Currency&to=$to_Currency";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-08
      • 2018-07-15
      • 2012-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-12
      • 2021-09-06
      相关资源
      最近更新 更多