PHP解析汇率提要XML [英] php parse exchange rate feed XML

查看:82
本文介绍了PHP解析汇率提要XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use the currentcy exchange rate feeds of the European Central Bank (ECB) http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml

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

They have provided documentation on how to parse the xml but none of the options works for me: I checked that allow_url_fopen=On is set.

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

例如,我使用了它,但是它没有回显任何东西,而且似乎$ XML对象始终为空.

For instance, I used but it doesn't echo anything and it seems the $XML object is always empty.

<?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
        //--------------------------------------------------
    }
?> 

更新:

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

As I am behind proxy at my test environment, I tried this but still I don't get to read the XML: function curl($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_close ($ch);
return 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,它不会抱怨缺少函数.)

I didn't find any relevant settings in php.ini. Check with phpinfo() if you have SimpleXML support and cURLsupport enabled. (You should have them both and especially SimpleXML since you're using it and it returns false, it doesn't complain about missing function.)

代理在这里可能是个问题.参见 cURL 可以解决您的问题.

Proxy might be an issue here. See this and this answer. Using cURL could be an answer to your problem.

这是另一个替代方法此处.

$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/>';
}

这篇关于PHP解析汇率提要XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆