【问题标题】:How to correctly parse and format XML in PHP?如何在 PHP 中正确解析和格式化 XML?
【发布时间】:2021-12-30 15:56:57
【问题描述】:

所以,我正在做一个任务,我需要获取当前欧元 - 美元之间的货币兑换,我必须使用 PHP 并且以前从未使用过它,所有数据都可以在这个网站上找到 https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml?5105e8233f9433cf70ac379d6ccc5775

到目前为止我的代码是

`<php
    
    ini_set('default_charset', 'UTF-8');
    $url = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml?5105e8233f9433cf70ac379d6ccc5775";
    $xml = simplexml_load_file($url);
    echo $xml->Cube;
    foreach ($xml as $record) {
        print_r($record);
    }

?>`

输出对我来说真的很令人困惑,因为它显示数组并且标签有“:”,如“gesmes:Envelope”,我不知道该怎么做,感谢任何帮助!

【问题讨论】:

    标签: php xml api


    【解决方案1】:

    您可以使用 XPath 直接选择您感兴趣的值:

    $url = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml?5105e8233f9433cf70ac379d6ccc5775";
    $xml = simplexml_load_file($url);
    $xml->registerXPathNamespace('c', "http://www.ecb.int/vocabulary/2002-08-01/eurofxref");
    
    $currencyTarget = 'USD' ;
    
    // look at all Cube elements (everywhere in the document) 
    // that have a attribute 'currency' equals to 'USD', 
    // and returns its rate attribute
    $rates = $xml->xpath("//c:Cube[@currency = '$currencyTarget']//@rate"); 
    
    // xpath returns an array, select the first (and unique) element, and convert the attribute to double
    $rate = doubleval($rates[0]) ; 
    
    var_dump($rate); // float(1.1271) 
    

    【讨论】:

      【解决方案2】:

      XML 确实有点混乱。
      我之前有过这个话题,所以我可以给你一个工作示例:

      编辑:我不确定你的网址上的哈希标签是什么。我认为你不需要它。

      ini_set('default_charset', 'UTF-8');
      $url = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml?5105e8233f9433cf70ac379d6ccc5775";
      $xml = simplexml_load_file($url, 'SimpleXMLElement', LIBXML_NOCDATA);
      if (!$xml instanceof \SimpleXMLElement) {
          throw new \Exception("Cannot get currency rates. Cannot parse Xml.");
      }
      if (!isset($xml->Cube->Cube->Cube)) {
          throw new \Exception("Cannot get currency rates. Xml path wrong.");
      }
      $rates = [];
      foreach ($xml->Cube->Cube->Cube as $rate) {
          $rates[strtoupper((string)$rate['currency'])] = (float)$rate['rate'];
      }
      echo var_export($rates, true) . PHP_EOL;
      

      结果:

      // array (
      //     'USD' => 1.1271,
      //     'JPY' => 128.22,
      //     'BGN' => 1.9558,
      //     'CZK' => 25.413,
      //     'DKK' => 7.4366,
      //     'GBP' => 0.83928,
      //     'HUF' => 367.8,
      //     'PLN' => 4.6818,
      //     'RON' => 4.9495,
      //     'SEK' => 10.096,
      //     'CHF' => 1.0462,
      //     'ISK' => 147.8,
      //     'NOK' => 10.0483,
      //     'HRK' => 7.516,
      //     'RUB' => 82.8124,
      //     'TRY' => 12.5247,
      //     'AUD' => 1.5581,
      //     'BRL' => 6.268,
      //     'CAD' => 1.4254,
      //     'CNY' => 7.2027,
      //     'HKD' => 8.7832,
      //     'IDR' => 16105.54,
      //     'ILS' => 3.4887,
      //     'INR' => 83.6905,
      //     'KRW' => 1344.64,
      //     'MXN' => 23.4637,
      //     'MYR' => 4.7152,
      //     'NZD' => 1.6098,
      //     'PHP' => 57.073,
      //     'SGD' => 1.5344,
      //     'THB' => 36.969,
      //     'ZAR' => 17.7513,
      // )
      

      编辑 2:您也可以看看 https://api.exchangerate.host/
      仅使用美元的示例调用:https://api.exchangerate.host/latest?base=EUR&amp;symbols=USD
      所有人的示例调用:https://api.exchangerate.host/latest?base=EUR
      如果我没记错的话,您可以使用日期(历史)而不是 latest

      【讨论】:

      • 谢谢你,有没有办法只能找回美元?我已经尝试将 XML 转换为 JSON,但似乎没有任何效果,我只需要检索 USD 的值并将其写入文件!
      • www.ecb.europa.eu 是欧盟官方 API。在那里你可以一次得到所有。 api.exchangerate.host 提供(历史)数据(我认为使用欧盟 API)。在那里你可以只请求一个(见上面的例子)。 IMO 两者都很好。我添加了两者,以便在一个失败时能够回退。 关于 JSON - 您可以通过 json_encode($array) 将 PHP 数组转换为 JSON。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-03
      • 2012-07-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多