simplexml_load_file未加载XML文件

时间:2018-09-17 15:37:33

标签: php xml

几天来,我一直在尝试解决此问题,但仍然无法解决问题所在。为什么不加载此XML文件的任何想法?我不断收到“ XML属性不可用或为空”消息。

true

我也尝试添加这样的名称空间,但无济于事:

$xml = simplexml_load_file('https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');

if(!isset($xml->Cube->Cube) || empty($xml->Cube->Cube)){
    echo 'XML attribute not available or empty';
}

我必须承认,PHP不是我的事。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

请尝试以下代码:

$file = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml';
function simplexml_load_file_curl($url) {
        $xml="";

        if(in_array('curl', get_loaded_extensions())){
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $xml = simplexml_load_string(curl_exec($ch));
            curl_close($ch);
        }
        else{
            $xml = simplexml_load_file($url);
        }

        return $xml;
    }


$xml = simplexml_load_file_curl($file);
echo '<pre>';print_r($xml);echo '</pre>';

reference

输出:

SimpleXMLElement Object
(
    [Cube] => SimpleXMLElement Object
        (
            [Cube] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [time] => 2018-09-17
                        )

                    [Cube] => Array
                        (
                            [0] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => USD
                                            [rate] => 1.1671
                                        )

                                )

                            [1] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => JPY
                                            [rate] => 130.79
                                        )

                                )

                            [2] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => BGN
                                            [rate] => 1.9558
                                        )

                                )

                            [3] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => CZK
                                            [rate] => 25.457
                                        )

                                )

                            [4] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => DKK
                                            [rate] => 7.4594
                                        )

                                )

                            [5] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => GBP
                                            [rate] => 0.88875
                                        )

                                )

                            [6] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => HUF
                                            [rate] => 324.50
                                        )

                                )

                            [7] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => PLN
                                            [rate] => 4.2955
                                        )

                                )

                            [8] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => RON
                                            [rate] => 4.6431
                                        )

                                )

                            [9] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => SEK
                                            [rate] => 10.4527
                                        )

                                )

                            [10] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => CHF
                                            [rate] => 1.1236
                                        )

                                )

                            [11] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => ISK
                                            [rate] => 127.80
                                        )

                                )

                            [12] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => NOK
                                            [rate] => 9.5620
                                        )

                                )

                            [13] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => HRK
                                            [rate] => 7.4298
                                        )

                                )

                            [14] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => RUB
                                            [rate] => 79.4113
                                        )

                                )

                            [15] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => TRY
                                            [rate] => 7.3332
                                        )

                                )

                            [16] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => AUD
                                            [rate] => 1.6258
                                        )

                                )

                            [17] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => BRL
                                            [rate] => 4.8942
                                        )

                                )

                            [18] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => CAD
                                            [rate] => 1.5193
                                        )

                                )

                            [19] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => CNY
                                            [rate] => 8.0139
                                        )

                                )

                            [20] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => HKD
                                            [rate] => 9.1579
                                        )

                                )

                            [21] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => IDR
                                            [rate] => 17351.27
                                        )

                                )

                            [22] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => ILS
                                            [rate] => 4.1866
                                        )

                                )

                            [23] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => INR
                                            [rate] => 84.6355
                                        )

                                )

                            [24] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => KRW
                                            [rate] => 1312.95
                                        )

                                )

                            [25] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => MXN
                                            [rate] => 21.9710
                                        )

                                )

                            [26] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => MYR
                                            [rate] => 4.8300
                                        )

                                )

                            [27] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => NZD
                                            [rate] => 1.7742
                                        )

                                )

                            [28] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => PHP
                                            [rate] => 63.211
                                        )

                                )

                            [29] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => SGD
                                            [rate] => 1.6017
                                        )

                                )

                            [30] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => THB
                                            [rate] => 38.071
                                        )

                                )

                            [31] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => ZAR
                                            [rate] => 17.4715
                                        )

                                )

                        )

                )

        )

)