Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php parse xml from url

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

like image 738
user2435262 Avatar asked Apr 27 '26 17:04

user2435262


2 Answers

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
                    )



                .
                .
                .

            )

    )

)
like image 81
mohammad mohsenipur Avatar answered Apr 30 '26 08:04

mohammad mohsenipur


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;
like image 38
Debashis Avatar answered Apr 30 '26 08:04

Debashis