0

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>";
    }
6
  • var_dump($xml->valcurs->valute) - post the result Commented Jan 15, 2014 at 3:54
  • @Rulisp You have checked that you $xml contains the data, right? Commented Jan 15, 2014 at 4:01
  • just a tip, not a solution: $xml = simplexml_load_file('http://cbr.ru/scripts/XML_daily.asp'); Commented Jan 15, 2014 at 4:04
  • @NewInTheBusiness of course. With print_r($xml) I have this. Commented Jan 15, 2014 at 4:05
  • @Rulisp Think you're sending the wrong header as it is xml...see my edit Commented Jan 15, 2014 at 4:06

2 Answers 2

2

Try this

foreach($xml->Valute as $val){
    echo "<p>".$val->NumCode."</p>";
}
Sign up to request clarification or add additional context in comments.

Comments

0

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>";
}

1 Comment

simplexml is not case sensative.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.