【问题标题】:Google Finance Convertor returns Undefined Offset 1Google 财经转换器返回未定义的偏移量 1
【发布时间】:2018-03-25 12:26:14
【问题描述】:

我正在尝试在我的项目中实现这个 Google Finance Converter。我实现了它,然后它工作正常。但是今天,当我重新检查我的应用程序以确保一切正常时,我发现 Google Finance Converter 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("&lt;/span&gt;",$get[1]);

请帮忙。

【问题讨论】:

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


    【解决方案1】:

    这是因为 google 已经停止提供金融 api 甚至 Finance.google.com 将我们带到谷歌金融标签

    【讨论】:

    • 您能添加一些网址或链接来证明您的回答吗?
    • 是的,您可以自己尝试链接
    • 哪个链接?你能补充一下你的答案吗?
    • 好的先生,请检查finance.google.com,因为它显示302作为状态码
    • 有什么替代方法吗?
    【解决方案2】:

    我也使用了这个 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);
    }
    

    希望这能持续一段时间。其他(免费)解决方案与美元绑定到有限的货币。如果您需要欧元兑菲律宾比索怎么办?

    【讨论】:

    • 这是一个 laravel 项目的一部分。选择您的需求,它应该可以工作。
    猜你喜欢
    • 2022-01-02
    • 2011-07-22
    • 2017-03-17
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-16
    相关资源
    最近更新 更多