php parse xml from url

24,515

Solution 1

you must put

 $Rate = $xml->Currencies->Currency['1']->Rate;

instead of

 $Rate = $xml->Currency[1]->Rate;

because of $xml structure is

   SimpleXMLElement Object
(
    [Date] => 20130530
    [Currencies] => SimpleXMLElement Object
    (
        [Currency] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [ID] => AED
                        [Units] => 1
                        [Rate] => 0.14900000
                    )

                [1] => SimpleXMLElement Object
                    (
                        [ID] => AUD
                        [Units] => 1
                        [Rate] => 0.52300000
                    )

                [2] => SimpleXMLElement Object
                    (
                        [ID] => BGN
                        [Units] => 1
                        [Rate] => 0.35900000
                    )

                [3] => SimpleXMLElement Object
                    (
                        [ID] => BYR
                        [Units] => 1000
                        [Rate] => 0.06290000
                    )



                .
                .
                .

            )

    )

)

Solution 2

You are missing 'Currencies' in your code. It should be:

$url = 'http://www.bank.lv/vk/xml.xml?date=20130530';
$xml = simplexml_load_file($url) or die("feed not loading");

$Rate = $xml->Currencies->Currency[1]->Rate;
echo $Rate;
Share:
24,515
user2435262
Author by

user2435262

Updated on May 30, 2020

Comments

  • user2435262
    user2435262 about 4 years

    http://www.bank.lv/vk/xml.xml?date=20130530

    
    
      $url = 'http://www.bank.lv/vk/xml.xml?date=20130530';
      $xml = simplexml_load_file($url) or die("feed not loading");
      $Rate = $xml->Currency[1]->Rate;
      echo $Rate;
      echo 'BREAK HTML';
      echo "-----";
      echo "// "; var_dump($xml); echo " //";
    

    Why HTML data dont output? Had tested lot of tutorials, but dont get, how to output data from this XML