2

欧州中央銀行 (ECB) 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

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

Update:

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

Please help me. Thanks!

4

2 に答える 2

3

に関連する設定が見つかりませんでしたphp.ini。サポートがあり、有効になっphpinfo()ているかどうかを確認してください。(特に、それを使用していて false を返すため、機能の欠落について文句を言うことはありません。)SimpleXMLcURLsupportSimpleXML

ここではプロキシが問題になる可能性があります。thisthis answerを参照してください。cURLを使用すると、問題が解決する可能性があります。


ここに 1 つの代替食品があります

$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/>';
}
于 2012-04-24T08:56:12.650 に答える