Google财经转换器返回未定义的偏移量1

时间:2018-03-25 12:26:14

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

我正在尝试在我的项目中实施此Google财务转换器。我实现了它,然后工作正常。但是今天当我重新检查我的应用程序以确保每件事情都正常工作时,我发现Google财务转换器API无效并且还返回错误。

PHP代码

/*********** Google Finance API ************/
  $from_Currency = "USD";
  $to_Currency = "INR";
  $encode_amount = $amount;
  $get = file_get_contents("http://finance.google.com/finance/converter?a=$encode_amount&from=$from_Currency&to=$to_Currency");
  $get = explode("<span class=bld>",$get);
  $get = explode("</span>",$get[1]);
  $converted_currency = preg_replace("/[^0-9\.]/", null, $get[0]);
  /*********** End Google Finance API ************/

返回类似Undefined offset:1

的内容

完成错误代码

 Notice: Undefined offset: 1 in E:\xampp\htdocs\sites\redapple\gateways\payumoney\payumoney.php on line 31

在我的代码中,此行代表第31行 $get = explode("</span>",$get[1]);

请帮忙。

2 个答案:

答案 0 :(得分:1)

这是因为google已经停止提供finace api,即使是finance.google.com也将我们带到谷歌财务标签

答案 1 :(得分:0)

我也使用过这个Google API。感谢duckduckgo(他们有货币转换器)找到了xe.com网站。正如Gurpreet Singh所说。

这是我的重写版本。显然它是课堂的一部分。

private static function convertCurrency($amount, $from, $to) {
    $url  = "https://www.xe.com/currencyconverter/convert/?Amount={$amount}&From={$from}&To={$to}";
    $data = @file_get_contents($url);
    if ( ! $data)
    {
        return null;
    }
    preg_match_all("/<span class='uccResultAmount'>([0-9.]*)<\/span>/", $data, $converted);
    try
    {
        $final = $converted[1][0];
    } catch (\Exception $e)
    {
        return null;
    }

    return round($final, 2);
}

让我们希望这种情况能够持续一段时间。其他(免费)解决方案与美元对有限货币的约束。如果您需要欧元兑菲律宾比索怎么办?