Rulisp
Rulisp

Reputation: 1736

Error while parsing xml file with php

I want to parse this page with php. I wrote this code, but it gives me an error - Invalid argument supplied for foreach()

    $opts = array('http' => array('header' => 'Accept-Charset: UTF-8, *;q=0'));
    $context = stream_context_create($opts);
    $data = file_get_contents('http://cbr.ru/scripts/XML_daily.asp',false, $context);
    $xml = simplexml_load_string($data);
    foreach($xml->valcurs->valute as $val){
        echo "<p>".$val->attributes()->numcode."</p>";
    }

Upvotes: 0

Views: 113

Answers (2)

rccoros
rccoros

Reputation: 590

Try this

foreach($xml->Valute as $val){
    echo "<p>".$val->NumCode."</p>";
}

Upvotes: 2

NewInTheBusiness
NewInTheBusiness

Reputation: 1475

Might be the header then:

    $opts  = stream_context_create(array('http' => array('header' => 'Accept:
application/xml')));

Still think you shouldn't grab attributes() though:

foreach($xml->ValCurs->Valute as $val) {
    echo "<p>".$val->NumCode."</p>";
}

Upvotes: 0

Related Questions