php解析汇率提要XML

2023-12-21

我正在尝试使用欧洲中央银行 (ECB) 的货币汇率源http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml

他们提供了有关如何解析 xml 的文档,但没有一个选项对我有用:我检查了allow_url_fopen=On 是否已设置。

http://www.ecb.int/stats/exchange/eurofxref/html/index.en.html http://www.ecb.int/stats/exchange/eurofxref/html/index.en.html

例如,我使用过,但它没有回显任何内容,并且 $XML 对象似乎始终为空。

<?php
    //This is aPHP(5)script example on how eurofxref-daily.xml can be parsed
    //Read eurofxref-daily.xml file in memory
    //For the next command you will need the config option allow_url_fopen=On (default)
    $XML=simplexml_load_file("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");
    //the file is updated daily between 2.15 p.m. and 3.00 p.m. CET

    foreach($XML->Cube->Cube->Cube as $rate){
        //Output the value of 1EUR for a currency code
        echo '1&euro;='.$rate["rate"].' '.$rate["currency"].'<br/>';
        //--------------------------------------------------
        //Here you can add your code for inserting
        //$rate["rate"] and $rate["currency"] into your database
        //--------------------------------------------------
    }
?> 

Update:

由于我在测试环境中使用代理,因此我尝试了此操作,但仍然无法读取 XML: 函数curl($url){ $ch=curl_init(); curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
卷曲关闭 ($ch);
返回curl_exec($ch); }

$address = urlencode($address); 
$data = curl("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml");  
$XML = simplexml_load_file($data);

var_dump($XML); -> returns boolean false

请帮我。谢谢!


我没有找到任何相关设置php.ini。检查与phpinfo()如果你有SimpleXML支持和cURLsupport已启用。 (你应该同时拥有它们,尤其是SimpleXML因为你正在使用它并且它返回 false,所以它不会抱怨缺少功能。)

代理可能是这里的一个问题。看this https://stackoverflow.com/questions/1366824/php-simplexml-failing-on-localhost and this https://stackoverflow.com/questions/5498587/proxy-problem-using-simplexml-load-file-in-php回答。使用cURL http://fi.php.net/curl可能是您问题的答案。


这是找到的一种替代方案here http://www.phpfreaks.com/forums/index.php?topic=325042.0.

$url = file_get_contents('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
$xml =  new SimpleXMLElement($url) ;

//file put contents - same as fopen, wrote  and close
//need to output "asXML" - simple xml returns an object based upon the raw xml
file_put_contents(dirname(__FILE__)."/loc.xml", $xml->asXML());

foreach($xml->Cube->Cube->Cube as $rate){
  echo '1&euro;='.$rate["rate"].' '.$rate["currency"].'<br/>';
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

php解析汇率提要XML 的相关文章

随机推荐

  • 每当 gem: 我设置 :output 但日志文件没有出现在我期望的位置

    In my schedule rb文件我有以下几行 set output log cron log log every 5 minutes do command echo hello end I ran whenever w正如这个问题所建
  • 如何让 Vim 突出显示匹配的括号?

    When I browse code in Vim I need to see opening and closing parenthesis brackets and pressing seems unproductive I tried
  • foreach 如何将对象转换为指定类型?

    我有一个问题foreach行为在C 我的自定义类实现了自定义GetEnumerator 该方法返回另一个object可以隐式转换为string 但是如果我这样做foreach string s in customClass 它在运行时失败
  • VFS:已达到文件最大限制 1231582

    我正在运行 Linux 2 6 36 内核 并且看到一些随机错误 像 ls error while loading shared libraries libpthread so 0 cannot open shared object fil
  • 使用扩展器进行 Knockout.js 验证 - 防止加载时验证

    我已经对 名字 实施了非常基本的必需验证 很大程度上基于淘汰赛网站上建议的示例 http knockoutjs com documentation extenders html http knockoutjs com documentati
  • 调整包含圆圈的图像映射的大小

    我正在尝试绘制下图中的所有数字 我已经完成了 但现在我想根据窗口的宽度动态调整图像和地图的大小 这是相关的html
  • 无法确定搁置数据库类型,whichdb 无法识别 gdb

    如果我尝试打开刚刚由 shelve 创建的文件 为什么 shelve 会引发错误 import shelve info file name Users bacon myproject temp test info info file she
  • 在结构中启动数组时遇到问题

    class CRA Account int tax 4 double refund 4 int SIN public CRA Account CRA Account CRA Account SIN 0 tax 4 0 refund 4 0
  • ASP.NET Core - System.Text.Json:如何拒绝有效负载中的未知属性?

    ASP NET Core 7 中的 Web API 与 System Text Json 我需要拒绝 PUT POST API 上的 JSON 有效负载 这些 API 指定了其他属性 这些属性不映射到模型中的任何属性 所以如果我的模型是 p
  • Angular UI Router - 使用 ui-sref 导航到动态状态时会出现双斜杠

    我正在创建一个 CMS 系统 因此我希望动态创建状态 由于您无法在配置阶段发出 http 请求 因此我决定在 run 函数中添加路由 如下所述 http blog brunscopelliti com how to defer route
  • 按列值过滤数据帧的行[重复]

    这个问题在这里已经有答案了 我有一个包含 10 列的数据框 一栏给出了鸟类的名称 实际上有 300 个物种 但我只对其中 200 个感兴趣 我只想保留这 200 个物种的信息 我的桌子截图 https i stack imgur com O
  • Hibernate中不同的保存方式有什么区别?

    Hibernate 有一些方法可以以某种方式获取您的对象并将其放入数据库中 它们之间有什么区别 何时使用哪个 为什么没有一种智能方法知道何时使用什么 到目前为止我已经确定的方法是 save update saveOrUpdate saveO
  • 转置一维 NumPy 数组

    我使用 Python 和 NumPy 并且在 转置 方面遇到一些问题 import numpy as np a np array 5 4 print a print a T 调用a T没有转置数组 如果a例如 然后它会正确转置 但我需要转置
  • 如何使用 scalaz.WriterT 记录 for 表达式?

    如何使用 scalaz WriterT 进行日志记录 关于 Monad 变压器 这是一个非常简短的介绍 您可能会找到更多信息哈斯克尔维基 http www haskell org haskellwiki Monad Transformers
  • 如何获取带有浅蓝色字段的 iPhone 地图应用程序蓝点当前位置?

    我认为标题是不言自明的 目前 当我为当前位置添加默认注释时 let currentAnnot MKPointAnnotation currentAnnot coordinate loc coordinate mainMap addAnnot
  • Eclipse kepler - 禁用 javascript 验证

    我刚刚安装了新的 eclipse kepler eclipse 4 3 它向我显示了来自第三方 javascript 库的数百个错误和警告 我尝试通过取消选中 首选项 JavaScript 验证器 中的 启用 JavaScript 语义验证
  • ptrdiff_t 太小?

    我一直想知道 不是吗ptrdiff t应该能够保存任意两个指针的差异根据定义 为什么当两个指针距离太远时会失败 我不是指任何特定的语言 我指的是具有这种类型的所有语言 例如 用地址减去指针1从带有地址的字节指针0xFFFFFFFF当你有 3
  • 在 AWS AMI Linux 服务器上设置 Supervisord [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我正在努力让主管工作以确保我的队列系统始终运行 以下是我从各种来源拼凑而成的步骤 以 root 或超级用户身份运行 1 easy inst
  • Postgres 时间戳

    我们正在争论在 postgres 中存储时间戳的最佳方式 目前 所有时间戳都存储为 00 并且我们有一个与每个客户端关联的时区 我们查找时区并转换发生某事的时间 这增加了复杂性 因为我们需要进行更多的连接和更复杂的查询 另一种方法是连接到
  • php解析汇率提要XML

    我正在尝试使用欧洲中央银行 ECB 的货币汇率源http www ecb int stats eurofxref eurofxref daily xml http www ecb int stats eurofxref eurofxref