James Sherman
James Sherman

Reputation: 1

Error Reading a YQL XML File

I'm trying to read and display some values from a YQL xml file. The XML is here:

http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22YHOO%22)%0A%09%09&diagnostics=false&format=xml&env=http%3A%2F%2Fdatatables.org%2Falltables.env

The PHP file I'm trying to use is below. The words I echoed are displaying, but no variable values from the XML file are.

Any help would be much appreciated! Thanks, James

Code:

<?php

$xml = simplexml_load_file('http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22YHOO%22)%0A%09%09&diagnostics=false&format=xml&env=http%3A%2F%2Fdatatables.org%2Falltables.env');

// iterate over query result set
echo '<h2>YHOO</h2>';
$results = $xml->results;
foreach ($results->quote as $q) {
  echo '<p>';
  echo 'Ask: ' . $q->Ask['ask'];
  echo "\n";
  echo 'Average Daily Volume: ' . $q->AverageDailyVolume['adv'];
  echo '</p>';
}  
?>

Upvotes: 0

Views: 243

Answers (1)

salathe
salathe

Reputation: 51960

You are making a couple of simple mistakes when trying to access the quote's values.

Incorrect

  • $q->Ask['ask']
  • $q->AverageDailyVolume['adv']

Correct

  • $q->Ask
  • $q->AverageDailyVolume

See the fixed example at codepad.viper-7.com/0Mp7VN

Upvotes: 0

Related Questions